1 /*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
4 * as indicated by the @author tags. See the copyright.txt file in the
5 * distribution for a full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22 package org.jboss.ha.singleton;
23
24 import java.io.IOException;
25 import java.net.URI;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.concurrent.CopyOnWriteArrayList;
29
30 import org.jboss.profileservice.spi.NoSuchProfileException;
31 import org.jboss.profileservice.spi.Profile;
32 import org.jboss.profileservice.spi.ProfileKey;
33 import org.jboss.profileservice.spi.ProfileService;
34 import org.jboss.profileservice.spi.metadata.ProfileMetaData;
35 import org.jboss.profileservice.spi.metadata.ProfileSourceMetaData;
36 import org.jboss.profileservice.spi.metadata.SubProfileMetaData;
37 import org.jboss.system.server.profile.repository.metadata.AbstractProfileSourceMetaData;
38 import org.jboss.system.server.profile.repository.metadata.BasicProfileMetaData;
39 import org.jboss.system.server.profile.repository.metadata.BasicSubProfileMetaData;
40 import org.jboss.system.server.profile.repository.metadata.HotDeploymentProfileSourceMetaData;
41 import org.jboss.system.server.profile.repository.metadata.ImmutableProfileSourceMetaData;
42 import org.jboss.system.server.profileservice.repository.AbstractProfileFactory;
43
44 /**
45 * Extends {@link HASingletonProfileActivator} by actually creating and
46 * registering a {@link Profile} from a configurable set of URIs during
47 * the {@link #start()} phase, deregistering it in the {@link #stop()} phase.
48 *
49 * @author Brian Stansberry
50 * @version $Revision: 85945 $
51 */
52 public class HASingletonProfileManager extends HASingletonProfileActivator implements HASingletonProfileManagerMBean
53 {
54 private AbstractProfileFactory profileFactory;
55
56 /** The list of URIs to scan */
57 private List<URI> uriList = new CopyOnWriteArrayList<URI>();
58
59 /**
60 * Create a new HASingletonProfileManager.
61 *
62 */
63 public HASingletonProfileManager()
64 {
65 super();
66 }
67
68 // ---------------------------------------------------------- Properties
69
70 public AbstractProfileFactory getProfileFactory()
71 {
72 return profileFactory;
73 }
74
75 public void setProfileFactory(AbstractProfileFactory profileFactory)
76 {
77 this.profileFactory = profileFactory;
78 }
79
80 /**
81 * Set the uri list
82 *
83 * @param list the list
84 * @throws IOException
85 */
86 public void setURIList(final List<URI> list) throws IOException
87 {
88 if (list == null)
89 {
90 throw new NullPointerException("list argument cannot be null");
91 }
92
93 // start out with a fresh list
94 uriList.clear();
95
96 for(int n = 0; n < list.size(); n ++)
97 {
98 URI uri = list.get(n);
99 if (uri == null)
100 {
101 throw new IllegalArgumentException("list element["+n+"] is null");
102 }
103
104 if( uriList.add(uri) == true )
105 {
106 log.debug("Added URI: " + uri);
107 }
108 }
109 log.debug("URI list: " + uriList);
110 }
111
112 /**
113 * Get the uri list
114 *
115 * @return the list
116 */
117 public List<URI> getURIList()
118 {
119 return new ArrayList<URI>(uriList);
120 }
121
122
123
124 // ----------------------------------------------------------------- Public
125
126 /**
127 * Builds a profile from the {@link #getURIList() URI list} and registers
128 * it under the configured {@link #getProfileKey()}.
129 */
130 public void start() throws Exception
131 {
132 if (this.profileFactory == null)
133 {
134 throw new IllegalStateException("Must configure profileFactory");
135 }
136
137 if (getProfileService() == null)
138 {
139 throw new IllegalStateException("Must configure profileService");
140 }
141
142 URI[] rootURIs = uriList.toArray(new URI[uriList.size()]);
143 // TODO add dependencies on bootstrap profiles
144 String[] rootSubProfiles = new String[0];
145 // Create a hotdeployment profile
146 ProfileMetaData metadata = createProfileMetaData(true, rootURIs, rootSubProfiles);
147
148 Profile profile = profileFactory.createProfile(getProfileKey(), metadata);
149 getProfileService().registerProfile(profile);
150 }
151
152 /**
153 * Unregisters the profile registered in {@link #start()}.
154 */
155 public void stop() throws Exception
156 {
157 ProfileService profSvc = getProfileService();
158 ProfileKey profKey = getProfileKey();
159 if (profSvc != null && profKey != null)
160 {
161 try
162 {
163 // Inactivate first if needed
164 if (profSvc.getActiveProfileKeys().contains(profKey))
165 {
166 releaseProfile();
167 }
168
169 profSvc.unregisterProfile(profKey);
170 }
171 catch (NoSuchProfileException e)
172 {
173 log.warn("Could not unregister unknown profile " + profKey);
174 }
175 }
176 }
177
178 // ---------------------------------------------------------------- Private
179
180
181 /**
182 * Create a profile meta data.
183 *
184 * @param name the profile name.
185 * @param repositoryType the repository type.
186 * @param uris the repository uris.
187 * @param subProfiles a list of profile dependencies.
188 * @return the profile meta data.
189 */
190 private ProfileMetaData createProfileMetaData(boolean hotDeployment, URI[] uris, String[] subProfiles)
191 {
192 // Create profile
193 BasicProfileMetaData metaData = new BasicProfileMetaData();
194 metaData.setDomain(getProfileDomain());
195 metaData.setServer(getProfileServer());
196 metaData.setName(getProfileName());
197
198 // Create profile sources
199 ProfileSourceMetaData source = createSource(uris, hotDeployment);
200 metaData.setSource(source);
201
202 List<SubProfileMetaData> profileList = new ArrayList<SubProfileMetaData>();
203 for(String subProfile : subProfiles)
204 {
205 BasicSubProfileMetaData md = new BasicSubProfileMetaData();
206 md.setName(subProfile);
207 profileList.add(md);
208 }
209 metaData.setSubprofiles(profileList);
210
211 return metaData;
212 }
213
214 /**
215 * Create a profile repository source meta data.
216 *
217 * @param type the repository type.
218 * @param uri the uri
219 * @return the profile source meta data.
220 */
221 protected ProfileSourceMetaData createSource(URI[] uris, boolean hotDeployment)
222 {
223 AbstractProfileSourceMetaData source = null;
224 if(hotDeployment)
225 {
226 source = new HotDeploymentProfileSourceMetaData();
227 }
228 else
229 {
230 source = new ImmutableProfileSourceMetaData();
231 }
232 List<String> sources = new ArrayList<String>();
233 for(URI uri : uris)
234 sources.add(uri.toString());
235 source.setSources(sources);
236 return source;
237 }
238
239 }