001: /*
002: * JBoss, a division of Red Hat
003: * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.portal.core.identity.service;
023:
024: import org.jboss.beans.metadata.plugins.AbstractBeanMetaData;
025: import org.jboss.kernel.Kernel;
026: import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap;
027: import org.jboss.kernel.spi.dependency.KernelControllerContext;
028: import org.jboss.portal.identity.IdentityContext;
029: import org.jboss.portal.identity.IdentityContextImpl;
030: import org.jboss.portal.identity.IdentityException;
031: import org.jboss.portal.identity.IdentityServiceController;
032: import org.jboss.portal.identity.ServiceJNDIBinder;
033: import org.jboss.portal.identity.boot.IdentityServiceLoader;
034: import org.jboss.portal.identity.event.IdentityEvent;
035: import org.jboss.portal.identity.event.IdentityEventBroadcaster;
036: import org.jboss.portal.identity.metadata.service.IdentityServicesMetaData;
037: import org.jboss.portal.identity.service.IdentityConfigurationService;
038: import org.jboss.portal.jems.as.JNDI;
039: import org.jboss.portal.jems.as.system.AbstractJBossService;
040: import org.jboss.portal.jems.as.system.JBossServiceModelMBean;
041:
042: import javax.management.ObjectName;
043:
044: /**
045: * @author <a href="mailto:boleslaw dot dawidowicz at jboss.org">Boleslaw Dawidowicz</a>
046: * @version $Revision: 8807 $
047: */
048: public class IdentityServiceControllerImpl extends AbstractJBossService
049: implements IdentityServiceController {
050:
051: private static final org.jboss.logging.Logger log = org.jboss.logging.Logger
052: .getLogger(IdentityServiceControllerImpl.class);
053:
054: private String jndiName;
055:
056: protected JNDI.Binding jndiBinding;
057:
058: private IdentityContext identityContext;
059:
060: private boolean registerMBeans = true;
061:
062: private String configFile;
063:
064: private String defaultConfigFile;
065:
066: private IdentityEventBroadcaster identityEventBroadcaster;
067:
068: /** . */
069: protected Kernel kernel;
070:
071: protected void startService() throws Exception {
072: if (jndiName != null) {
073: jndiBinding = new JNDI.Binding(jndiName, this );
074: jndiBinding.bind();
075: }
076:
077: //initialize microcontainer stuff
078: try {
079: BasicBootstrap bootstrap = new BasicBootstrap();
080: bootstrap.run();
081: kernel = bootstrap.getKernel();
082: } catch (Exception e) {
083: throw new IdentityException(e);
084: }
085:
086: IdentityServicesMetaData servicesMetaData = new IdentityServicesMetaData(
087: defaultConfigFile, configFile);
088:
089: identityContext = bootstrapIdentityContext();
090:
091: // IdentityEventBroadcaster
092: IdentityEventBroadcaster broadcaster = identityEventBroadcaster;
093: if (broadcaster == null) {
094: broadcaster = new IdentityEventBroadcaster() {
095: public void fireEvent(IdentityEvent event) {
096: // Noop
097: }
098: };
099: }
100:
101: //
102: try {
103: identityContext.register(broadcaster,
104: IdentityContext.TYPE_IDENTITY_EVENT_BROADCASTER);
105: } catch (Throwable throwable) {
106: throw new IdentityException(
107: "Unable to install IdentityEventBroadcaster",
108: throwable);
109: }
110:
111: //process the list of modules, instantiate them, configure them, tide them
112: try {
113:
114: //inject configuration service
115: IdentityConfigurationService configuration = servicesMetaData
116: .getConfigurationService();
117: configuration.setIdentityContext(identityContext);
118: //TODO:set proper jndiName and serviceName and JNDI binder
119: configuration.start();
120:
121: IdentityServiceLoader serviceLoader = new IdentityServiceLoader(
122: identityContext, kernel, registerMBeans) {
123:
124: protected void registerMBean(String serviceName,
125: Object serviceObject) throws Exception {
126: JBossServiceModelMBean mbean = new JBossServiceModelMBean(
127: serviceObject);
128: getServer().registerMBean(mbean,
129: new ObjectName(serviceName));
130: }
131:
132: protected void unregisterMBean(String serviceName)
133: throws Exception {
134: ObjectName on = new ObjectName(serviceName);
135: if (getServer().isRegistered(on)) {
136: getServer().unregisterMBean(on);
137: }
138: }
139:
140: protected ServiceJNDIBinder getServiceJNDIBinder()
141: throws Exception {
142: return new SimpleServiceJNDIBinder();
143: }
144: };
145:
146: // process datasources and modules
147: serviceLoader.bootstrapDatasource(servicesMetaData
148: .getDatasourceServices().getDatasourcesList());
149:
150: serviceLoader.bootstrapModules(servicesMetaData
151: .getModuleServices().getModulesList());
152:
153: } catch (Throwable e) {
154: throw new IdentityException(
155: "Cannot initiate identity modules: ", e);
156: }
157: }
158:
159: protected void stopService() throws Exception {
160: if (jndiBinding != null) {
161: jndiBinding.unbind();
162: jndiBinding = null;
163: }
164:
165: }
166:
167: private IdentityContext bootstrapIdentityContext() throws Exception {
168: KernelControllerContext identityKernelContext;
169: try {
170: AbstractBeanMetaData contextBMD = new AbstractBeanMetaData(
171: "portal:identity=IdentityContext",
172: IdentityContextImpl.class.getName());
173: //beans.add(contextBMD);
174: identityKernelContext = kernel.getController().install(
175: contextBMD);
176: return (IdentityContext) identityKernelContext.getTarget();
177:
178: } catch (Throwable throwable) {
179: throw new IdentityException(
180: "Unable to install IdentityContext", throwable);
181: }
182:
183: }
184:
185: public IdentityContext getIdentityContext() {
186: return identityContext;
187: }
188:
189: public String getConfigFile() {
190: return configFile;
191: }
192:
193: public void setConfigFile(String configFile) {
194: this .configFile = configFile;
195: }
196:
197: public String getDefaultConfigFile() {
198: return defaultConfigFile;
199: }
200:
201: public void setDefaultConfigFile(String defaultConfigFile) {
202: this .defaultConfigFile = defaultConfigFile;
203: }
204:
205: public String getJndiName() {
206: return jndiName;
207: }
208:
209: public void setJndiName(String jndiName) {
210: this .jndiName = jndiName;
211: }
212:
213: public JNDI.Binding getJndiBinding() {
214: return jndiBinding;
215: }
216:
217: public void setJndiBinding(JNDI.Binding jndiBinding) {
218: this .jndiBinding = jndiBinding;
219: }
220:
221: public boolean isRegisterMBeans() {
222: return registerMBeans;
223: }
224:
225: public void setRegisterMBeans(boolean registerMBeans) {
226: this .registerMBeans = registerMBeans;
227: }
228:
229: public IdentityEventBroadcaster getIdentityEventBroadcaster() {
230: return identityEventBroadcaster;
231: }
232:
233: public void setIdentityEventBroadcaster(
234: IdentityEventBroadcaster identityEventBroadcaster) {
235: this.identityEventBroadcaster = identityEventBroadcaster;
236: }
237: }
|