001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.kernel.basic;
017:
018: import java.util.Date;
019: import java.util.HashSet;
020: import java.util.Iterator;
021: import java.util.LinkedList;
022: import java.util.Set;
023: import javax.management.ObjectName;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.geronimo.gbean.GBeanData;
028: import org.apache.geronimo.gbean.GBeanInfo;
029: import org.apache.geronimo.gbean.AbstractNameQuery;
030: import org.apache.geronimo.gbean.AbstractName;
031: import org.apache.geronimo.gbean.runtime.GBeanInstance;
032: import org.apache.geronimo.gbean.runtime.LifecycleBroadcaster;
033: import org.apache.geronimo.kernel.DependencyManager;
034: import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
035: import org.apache.geronimo.kernel.GBeanNotFoundException;
036: import org.apache.geronimo.kernel.InternalKernelException;
037: import org.apache.geronimo.kernel.Kernel;
038: import org.apache.geronimo.kernel.KernelGBean;
039: import org.apache.geronimo.kernel.KernelRegistry;
040: import org.apache.geronimo.kernel.NoSuchAttributeException;
041: import org.apache.geronimo.kernel.NoSuchOperationException;
042: import org.apache.geronimo.kernel.Jsr77Naming;
043: import org.apache.geronimo.kernel.Naming;
044: import org.apache.geronimo.kernel.management.State;
045: import org.apache.geronimo.kernel.lifecycle.LifecycleMonitor;
046: import org.apache.geronimo.kernel.proxy.ProxyManager;
047:
048: /**
049: * The core of a Geronimo instance.
050: * A Kernel is responsible for managing the Configurations that comprise a
051: * Geronimo system and exposing them using JMX. Each Kernel is associated
052: * with an MBeanServer that is used to register the Configurations themselves
053: * and the MBeans they define.
054: * <p/>
055: * Dependencies between MBeans are handled by a dedicated DependencyManager
056: * that is responsible for tracking those dependencies and ensuring that the
057: * dependent objects follow the appropriate lifecycle and receive appropriate
058: * notifications.
059: * <p/>
060: * The Kernel also provides a ConfigurationStore which is used to stage
061: * installed Configurations (providing a local filesystem based classpath) and
062: * used hold the persistent state of each Configuration. This allows
063: * Configurations to restart in the event of system failure.
064: *
065: * TODO: Describe the order of method invocation (e.g. if loadGbean may be before boot)
066: *
067: * @version $Rev:386276 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
068: */
069: public class BasicKernel implements Kernel {
070: /**
071: * Helper objects for invoke and getAttribute
072: */
073: private static final String[] NO_TYPES = new String[0];
074: private static final Object[] NO_ARGS = new Object[0];
075:
076: /**
077: * Name of this kernel
078: */
079: private final String kernelName;
080:
081: /**
082: * The log
083: */
084: private Log log;
085:
086: /**
087: * Is this kernel running?
088: */
089: private boolean running;
090:
091: /**
092: * The timestamp when the kernel was started
093: */
094: private Date bootTime;
095:
096: /**
097: * The registry
098: */
099: private final BasicRegistry registry;
100:
101: /**
102: * Listeners for when the kernel shutdown
103: */
104: private final LinkedList shutdownHooks = new LinkedList();
105:
106: /**
107: * This manager is used by the kernel to manage dependencies between gbeans
108: */
109: private DependencyManager dependencyManager;
110:
111: /**
112: * Monitors the lifecycle of all gbeans.
113: */
114: private BasicLifecycleMonitor lifecycleMonitor;
115: private LifecycleMonitor publicLifecycleMonitor;
116:
117: /**
118: * This factory gbean proxies, and tracks all proxies in the system
119: */
120: private ProxyManager proxyManager;
121:
122: private static final Naming INSTANCE = new Jsr77Naming();
123:
124: /**
125: * Construct a Kernel with the specified name.
126: *
127: * @param kernelName the name of the kernel
128: */
129: public BasicKernel(String kernelName) {
130: if (kernelName.indexOf(':') >= 0
131: || kernelName.indexOf('*') >= 0
132: || kernelName.indexOf('?') >= 0) {
133: throw new IllegalArgumentException(
134: "Kernel name may not contain a ':', '*' or '?' character");
135: }
136: this .kernelName = kernelName;
137: this .registry = new BasicRegistry();
138: }
139:
140: public String getKernelName() {
141: return kernelName;
142: }
143:
144: public Naming getNaming() {
145: return INSTANCE;
146: }
147:
148: /**
149: * @deprecated this will be removed as when we add generalized dependencies to gbeans... the only current user is Configuration
150: */
151: public DependencyManager getDependencyManager() {
152: return dependencyManager;
153: }
154:
155: /**
156: * Gets the lifecycle monitor.
157: * @deprecated don't use this yet... it may change or go away
158: */
159: public LifecycleMonitor getLifecycleMonitor() {
160: return publicLifecycleMonitor;
161: }
162:
163: /**
164: * Gets the proxy manager.
165: * @deprecated don't use this yet... it may change or go away
166: */
167: public ProxyManager getProxyManager() {
168: return proxyManager;
169: }
170:
171: public Object getAttribute(ObjectName objectName,
172: String attributeName) throws GBeanNotFoundException,
173: NoSuchAttributeException, Exception {
174: GBeanInstance gbeanInstance = registry
175: .getGBeanInstance(objectName);
176: return gbeanInstance.getAttribute(attributeName);
177: }
178:
179: public Object getAttribute(AbstractName abstractName,
180: String attributeName) throws GBeanNotFoundException,
181: NoSuchAttributeException, Exception {
182: GBeanInstance gbeanInstance = registry
183: .getGBeanInstance(abstractName);
184: return gbeanInstance.getAttribute(attributeName);
185: }
186:
187: public Object getAttribute(String shortName, String attributeName)
188: throws GBeanNotFoundException, NoSuchAttributeException,
189: Exception {
190: return getAttribute(shortName, null, attributeName);
191: }
192:
193: public Object getAttribute(Class type, String attributeName)
194: throws GBeanNotFoundException, NoSuchAttributeException,
195: Exception {
196: return getAttribute(null, type, attributeName);
197: }
198:
199: public Object getAttribute(String shortName, Class type,
200: String attributeName) throws GBeanNotFoundException,
201: NoSuchAttributeException, Exception {
202: GBeanInstance gbeanInstance = registry.getGBeanInstance(
203: shortName, type);
204: return gbeanInstance.getAttribute(attributeName);
205: }
206:
207: public void setAttribute(AbstractName abstractName,
208: String attributeName, Object attributeValue)
209: throws GBeanNotFoundException, NoSuchAttributeException,
210: Exception {
211: GBeanInstance gbeanInstance = registry
212: .getGBeanInstance(abstractName);
213: gbeanInstance.setAttribute(attributeName, attributeValue);
214: }
215:
216: public void setAttribute(String shortName, String attributeName,
217: Object attributeValue) throws GBeanNotFoundException,
218: NoSuchAttributeException, Exception {
219: setAttribute(shortName, null, attributeName, attributeValue);
220: }
221:
222: public void setAttribute(Class type, String attributeName,
223: Object attributeValue) throws GBeanNotFoundException,
224: NoSuchAttributeException, Exception {
225: setAttribute(null, type, attributeName, attributeValue);
226: }
227:
228: public void setAttribute(String shortName, Class type,
229: String attributeName, Object attributeValue)
230: throws GBeanNotFoundException, NoSuchAttributeException,
231: Exception {
232: GBeanInstance gbeanInstance = registry.getGBeanInstance(
233: shortName, type);
234: gbeanInstance.setAttribute(attributeName, attributeValue);
235: }
236:
237: public Object invoke(ObjectName objectName, String methodName)
238: throws GBeanNotFoundException, NoSuchOperationException,
239: InternalKernelException, Exception {
240: return invoke(objectName, methodName, NO_ARGS, NO_TYPES);
241: }
242:
243: public Object invoke(AbstractName abstractName, String methodName)
244: throws GBeanNotFoundException, NoSuchOperationException,
245: InternalKernelException, Exception {
246: return invoke(abstractName, methodName, NO_ARGS, NO_TYPES);
247: }
248:
249: public Object invoke(String shortName, String methodName)
250: throws GBeanNotFoundException, NoSuchOperationException,
251: InternalKernelException, Exception {
252: return invoke(shortName, null, methodName, NO_ARGS, NO_TYPES);
253: }
254:
255: public Object invoke(Class type, String methodName)
256: throws GBeanNotFoundException, NoSuchOperationException,
257: InternalKernelException, Exception {
258: return invoke(null, type, methodName, NO_ARGS, NO_TYPES);
259: }
260:
261: public Object invoke(String shortName, Class type, String methodName)
262: throws GBeanNotFoundException, NoSuchOperationException,
263: InternalKernelException, Exception {
264: return invoke(shortName, type, methodName, NO_ARGS, NO_TYPES);
265: }
266:
267: public Object invoke(ObjectName objectName, String methodName,
268: Object[] args, String[] types)
269: throws GBeanNotFoundException, NoSuchOperationException,
270: InternalKernelException, Exception {
271: GBeanInstance gbeanInstance = registry
272: .getGBeanInstance(objectName);
273: return gbeanInstance.invoke(methodName, args, types);
274: }
275:
276: public Object invoke(AbstractName abstractName, String methodName,
277: Object[] args, String[] types)
278: throws GBeanNotFoundException, NoSuchOperationException,
279: InternalKernelException, Exception {
280: GBeanInstance gbeanInstance = registry
281: .getGBeanInstance(abstractName);
282: return gbeanInstance.invoke(methodName, args, types);
283: }
284:
285: public Object invoke(String shortName, String methodName,
286: Object[] args, String[] types)
287: throws GBeanNotFoundException, NoSuchOperationException,
288: InternalKernelException, Exception {
289: return invoke(shortName, null, methodName, args, types);
290: }
291:
292: public Object invoke(Class type, String methodName, Object[] args,
293: String[] types) throws GBeanNotFoundException,
294: NoSuchOperationException, InternalKernelException,
295: Exception {
296: return invoke(null, type, methodName, args, types);
297: }
298:
299: public Object invoke(String shortName, Class type,
300: String methodName, Object[] args, String[] types)
301: throws GBeanNotFoundException, NoSuchOperationException,
302: InternalKernelException, Exception {
303: GBeanInstance gbeanInstance = registry.getGBeanInstance(
304: shortName, type);
305: return gbeanInstance.invoke(methodName, args, types);
306: }
307:
308: public boolean isLoaded(AbstractName name) {
309: return registry.isRegistered(name);
310: }
311:
312: public boolean isLoaded(String shortName) {
313: return isLoaded(shortName, null);
314: }
315:
316: public boolean isLoaded(Class type) {
317: return isLoaded(null, type);
318: }
319:
320: public boolean isLoaded(String shortName, Class type) {
321: try {
322: registry.getGBeanInstance(shortName, type);
323: return true;
324: } catch (GBeanNotFoundException e) {
325: // Dain: yes this is flow control using exceptions, but I'm too lazy to add another isRegistered method to the basic registry
326: return false;
327: }
328: }
329:
330: public Object getGBean(String shortName)
331: throws GBeanNotFoundException, InternalKernelException,
332: IllegalStateException {
333: return getGBean(shortName, null);
334: }
335:
336: public Object getGBean(Class type) throws GBeanNotFoundException,
337: InternalKernelException, IllegalStateException {
338: return getGBean(null, type);
339: }
340:
341: public Object getGBean(String shortName, Class type)
342: throws GBeanNotFoundException, InternalKernelException,
343: IllegalStateException {
344: GBeanInstance gbeanInstance = registry.getGBeanInstance(
345: shortName, type);
346: if (gbeanInstance.getState() != State.RUNNING_INDEX) {
347: throw new IllegalStateException("GBean is not running: "
348: + gbeanInstance.getAbstractName());
349: }
350: return gbeanInstance.getTarget();
351: }
352:
353: public Object getGBean(ObjectName name)
354: throws GBeanNotFoundException, InternalKernelException,
355: IllegalStateException {
356: GBeanInstance gbeanInstance = registry.getGBeanInstance(name);
357: if (gbeanInstance.getState() != State.RUNNING_INDEX) {
358: throw new IllegalStateException("GBean is not running: "
359: + name);
360: }
361: return gbeanInstance.getTarget();
362: }
363:
364: public Object getGBean(AbstractName name)
365: throws GBeanNotFoundException, InternalKernelException,
366: IllegalStateException {
367: GBeanInstance gbeanInstance = registry.getGBeanInstance(name);
368: if (gbeanInstance.getState() != State.RUNNING_INDEX) {
369: throw new IllegalStateException("GBean is not running: "
370: + name);
371: }
372: return gbeanInstance.getTarget();
373: }
374:
375: public GBeanInfo getGBeanInfo(ObjectName name)
376: throws GBeanNotFoundException {
377: GBeanInstance gbeanInstance = registry.getGBeanInstance(name);
378: return gbeanInstance.getGBeanInfo();
379: }
380:
381: public GBeanInfo getGBeanInfo(AbstractName name)
382: throws GBeanNotFoundException {
383: GBeanInstance gbeanInstance = registry.getGBeanInstance(name);
384: return gbeanInstance.getGBeanInfo();
385: }
386:
387: public GBeanInfo getGBeanInfo(String shortName)
388: throws GBeanNotFoundException {
389: return getGBeanInfo(shortName, null);
390: }
391:
392: public GBeanInfo getGBeanInfo(Class type)
393: throws GBeanNotFoundException {
394: return getGBeanInfo(null, type);
395: }
396:
397: public GBeanInfo getGBeanInfo(String shortName, Class type)
398: throws GBeanNotFoundException {
399: GBeanInstance gbeanInstance = registry.getGBeanInstance(
400: shortName, type);
401: return gbeanInstance.getGBeanInfo();
402: }
403:
404: public GBeanData getGBeanData(AbstractName name)
405: throws GBeanNotFoundException, InternalKernelException {
406: GBeanInstance gbeanInstance = registry.getGBeanInstance(name);
407: return gbeanInstance.getGBeanData();
408: }
409:
410: public GBeanData getGBeanData(String shortName)
411: throws GBeanNotFoundException, InternalKernelException {
412: return getGBeanData(shortName, null);
413: }
414:
415: public GBeanData getGBeanData(Class type)
416: throws GBeanNotFoundException, InternalKernelException {
417: return getGBeanData(null, type);
418: }
419:
420: public GBeanData getGBeanData(String shortName, Class type)
421: throws GBeanNotFoundException, InternalKernelException {
422: GBeanInstance gbeanInstance = registry.getGBeanInstance(
423: shortName, type);
424: return gbeanInstance.getGBeanData();
425: }
426:
427: public void loadGBean(GBeanData gbeanData, ClassLoader classLoader)
428: throws GBeanAlreadyExistsException, InternalKernelException {
429: AbstractName abstractName = gbeanData.getAbstractName();
430: Set interfaces = gbeanData.getGBeanInfo().getInterfaces();
431: LifecycleBroadcaster lifecycleBroadcaster = lifecycleMonitor
432: .createLifecycleBroadcaster(abstractName, interfaces);
433: GBeanInstance gbeanInstance = new GBeanInstance(gbeanData,
434: this , dependencyManager, lifecycleBroadcaster,
435: classLoader);
436: registry.register(gbeanInstance);
437: lifecycleBroadcaster.fireLoadedEvent();
438: }
439:
440: public void startGBean(AbstractName name)
441: throws GBeanNotFoundException, InternalKernelException,
442: IllegalStateException {
443: GBeanInstance gbeanInstance = registry.getGBeanInstance(name);
444: gbeanInstance.start();
445: }
446:
447: public void startGBean(String shortName)
448: throws GBeanNotFoundException, InternalKernelException,
449: IllegalStateException {
450: startGBean(shortName, null);
451: }
452:
453: public void startGBean(Class type) throws GBeanNotFoundException,
454: InternalKernelException, IllegalStateException {
455: startGBean(null, type);
456: }
457:
458: public void startGBean(String shortName, Class type)
459: throws GBeanNotFoundException, InternalKernelException,
460: IllegalStateException {
461: GBeanInstance gbeanInstance = registry.getGBeanInstance(
462: shortName, type);
463: gbeanInstance.start();
464: }
465:
466: public void startRecursiveGBean(AbstractName name)
467: throws GBeanNotFoundException, InternalKernelException,
468: IllegalStateException {
469: GBeanInstance gbeanInstance = registry.getGBeanInstance(name);
470: gbeanInstance.startRecursive();
471: }
472:
473: public void startRecursiveGBean(String shortName)
474: throws GBeanNotFoundException, InternalKernelException,
475: IllegalStateException {
476: startRecursiveGBean(shortName, null);
477: }
478:
479: public void startRecursiveGBean(Class type)
480: throws GBeanNotFoundException, InternalKernelException,
481: IllegalStateException {
482: startRecursiveGBean(null, type);
483: }
484:
485: public void startRecursiveGBean(String shortName, Class type)
486: throws GBeanNotFoundException, InternalKernelException,
487: IllegalStateException {
488: GBeanInstance gbeanInstance = registry.getGBeanInstance(
489: shortName, type);
490: gbeanInstance.startRecursive();
491: }
492:
493: public boolean isRunning(AbstractName name) {
494: try {
495: GBeanInstance gbeanInstance = registry
496: .getGBeanInstance(name);
497: return gbeanInstance.getState() == State.RUNNING_INDEX;
498: } catch (GBeanNotFoundException e) {
499: return false;
500: }
501: }
502:
503: public boolean isRunning(String shortName) {
504: return isRunning(shortName, null);
505: }
506:
507: public boolean isRunning(Class type) {
508: return isRunning(null, type);
509: }
510:
511: public boolean isRunning(String shortName, Class type) {
512: try {
513: GBeanInstance gbeanInstance = registry.getGBeanInstance(
514: shortName, type);
515: return gbeanInstance.getState() == State.RUNNING_INDEX;
516: } catch (GBeanNotFoundException e) {
517: return false;
518: }
519: }
520:
521: public void stopGBean(AbstractName name)
522: throws GBeanNotFoundException, InternalKernelException,
523: IllegalStateException {
524: GBeanInstance gbeanInstance = registry.getGBeanInstance(name);
525: gbeanInstance.stop();
526: }
527:
528: public void stopGBean(String shortName)
529: throws GBeanNotFoundException, InternalKernelException,
530: IllegalStateException {
531: stopGBean(shortName, null);
532: }
533:
534: public void stopGBean(Class type) throws GBeanNotFoundException,
535: InternalKernelException, IllegalStateException {
536: stopGBean(null, type);
537: }
538:
539: public void stopGBean(String shortName, Class type)
540: throws GBeanNotFoundException, InternalKernelException,
541: IllegalStateException {
542: GBeanInstance gbeanInstance = registry.getGBeanInstance(
543: shortName, type);
544: gbeanInstance.stop();
545: }
546:
547: public void unloadGBean(AbstractName name)
548: throws GBeanNotFoundException, InternalKernelException,
549: IllegalStateException {
550: GBeanInstance gbeanInstance = registry.getGBeanInstance(name);
551: gbeanInstance.die();
552: registry.unregister(name);
553: }
554:
555: public void unloadGBean(String shortName)
556: throws GBeanNotFoundException, InternalKernelException,
557: IllegalStateException {
558: unloadGBean(shortName, null);
559: }
560:
561: public void unloadGBean(Class type) throws GBeanNotFoundException,
562: InternalKernelException, IllegalStateException {
563: unloadGBean(null, type);
564: }
565:
566: public void unloadGBean(String shortName, Class type)
567: throws GBeanNotFoundException, InternalKernelException,
568: IllegalStateException {
569: GBeanInstance gbeanInstance = registry.getGBeanInstance(
570: shortName, type);
571: AbstractName name = gbeanInstance.getAbstractName();
572: gbeanInstance.die();
573: registry.unregister(name);
574: }
575:
576: public int getGBeanState(ObjectName name)
577: throws GBeanNotFoundException {
578: GBeanInstance gbeanInstance = registry.getGBeanInstance(name);
579: return gbeanInstance.getState();
580: }
581:
582: public int getGBeanState(AbstractName name)
583: throws GBeanNotFoundException {
584: GBeanInstance gbeanInstance = registry.getGBeanInstance(name);
585: return gbeanInstance.getState();
586: }
587:
588: public int getGBeanState(String shortName)
589: throws GBeanNotFoundException {
590: return getGBeanState(shortName, null);
591: }
592:
593: public int getGBeanState(Class type) throws GBeanNotFoundException {
594: return getGBeanState(null, type);
595: }
596:
597: public int getGBeanState(String shortName, Class type)
598: throws GBeanNotFoundException {
599: GBeanInstance gbeanInstance = registry.getGBeanInstance(
600: shortName, type);
601: return gbeanInstance.getState();
602: }
603:
604: public long getGBeanStartTime(AbstractName name)
605: throws GBeanNotFoundException {
606: GBeanInstance gbeanInstance = registry.getGBeanInstance(name);
607: return gbeanInstance.getStartTime();
608: }
609:
610: public long getGBeanStartTime(String shortName)
611: throws GBeanNotFoundException {
612: return getGBeanStartTime(shortName, null);
613: }
614:
615: public long getGBeanStartTime(Class type)
616: throws GBeanNotFoundException {
617: return getGBeanStartTime(null, type);
618: }
619:
620: public long getGBeanStartTime(String shortName, Class type)
621: throws GBeanNotFoundException {
622: GBeanInstance gbeanInstance = registry.getGBeanInstance(
623: shortName, type);
624: return gbeanInstance.getStartTime();
625: }
626:
627: public Set listGBeans(ObjectName pattern) {
628: Set gbeans = registry.listGBeans(pattern);
629:
630: Set result = new HashSet(gbeans.size());
631: for (Iterator i = gbeans.iterator(); i.hasNext();) {
632: GBeanInstance instance = (GBeanInstance) i.next();
633: result.add(instance.getObjectNameObject());
634: }
635: return result;
636: }
637:
638: public Set listGBeans(Set patterns) {
639: Set gbeans = new HashSet();
640: for (Iterator iterator = patterns.iterator(); iterator
641: .hasNext();) {
642: Object pattern = iterator.next();
643: if (pattern instanceof ObjectName) {
644: gbeans.addAll(listGBeans((ObjectName) pattern));
645: } else if (pattern instanceof AbstractNameQuery) {
646: gbeans.addAll(listGBeans((AbstractNameQuery) pattern));
647: }
648: }
649: return gbeans;
650: }
651:
652: public Set listGBeans(AbstractNameQuery query) {
653: Set gbeans = registry.listGBeans(query);
654: Set result = new HashSet(gbeans.size());
655: for (Iterator i = gbeans.iterator(); i.hasNext();) {
656: GBeanInstance instance = (GBeanInstance) i.next();
657: result.add(instance.getAbstractName());
658: }
659: return result;
660: }
661:
662: public Set listGBeansByInterface(String[] interfaces) {
663: Set gbeans = new HashSet();
664: Set all = listGBeans((AbstractNameQuery) null);
665: for (Iterator it = all.iterator(); it.hasNext();) {
666: AbstractName name = (AbstractName) it.next();
667: try {
668: GBeanInfo info = getGBeanInfo(name);
669: Set intfs = info.getInterfaces();
670: for (int i = 0; i < interfaces.length; i++) {
671: String candidate = interfaces[i];
672: if (intfs.contains(candidate)) {
673: gbeans.add(name);
674: break;
675: }
676: }
677: } catch (GBeanNotFoundException e) {
678: }
679: }
680: return gbeans;
681: }
682:
683: public AbstractName getAbstractNameFor(Object service) {
684: if (!running) {
685: return null;
686: }
687:
688: // check if service is a proxy
689: AbstractName name = proxyManager.getProxyTarget(service);
690: if (name != null) {
691: return name;
692: }
693:
694: // try the registry
695: GBeanInstance gbeanInstance = registry
696: .getGBeanInstanceByInstance(service);
697: if (gbeanInstance != null) {
698: return gbeanInstance.getAbstractName();
699: }
700:
701: // didn't fing the name
702: return null;
703: }
704:
705: public String getShortNameFor(Object service) {
706: AbstractName name = getAbstractNameFor(service);
707: if (name != null) {
708: return (String) name.getName().get("name");
709: }
710: return null;
711: }
712:
713: /**
714: * Boot this Kernel, triggering the instantiation of the MBeanServer and DependencyManager,
715: * and the registration of ConfigurationStore
716: *
717: * @throws java.lang.Exception if the boot fails
718: */
719: public void boot() throws Exception {
720: if (running) {
721: return;
722: }
723: bootTime = new Date();
724: log = LogFactory.getLog(BasicKernel.class.getName());
725: log.debug("Starting boot");
726:
727: // todo cleanup when boot fails
728: KernelRegistry.registerKernel(this );
729:
730: registry.start(this );
731:
732: lifecycleMonitor = new BasicLifecycleMonitor(this );
733: publicLifecycleMonitor = new LifecycleMonitorFlyweight(
734: lifecycleMonitor);
735: dependencyManager = new BasicDependencyManager(
736: publicLifecycleMonitor);
737: proxyManager = new BasicProxyManager(this );
738:
739: // load and start the kernel gbean
740: GBeanData kernelGBeanData = new GBeanData(KERNEL_NAME,
741: KernelGBean.GBEAN_INFO);
742: loadGBean(kernelGBeanData, getClass().getClassLoader());
743: startGBean(KERNEL_NAME);
744:
745: running = true;
746: log.debug("Booted");
747: }
748:
749: public Date getBootTime() {
750: return bootTime;
751: }
752:
753: public void registerShutdownHook(Runnable hook) {
754: assert hook != null : "Shutdown hook was null";
755: synchronized (shutdownHooks) {
756: shutdownHooks.add(hook);
757: }
758: }
759:
760: public void unregisterShutdownHook(Runnable hook) {
761: synchronized (shutdownHooks) {
762: shutdownHooks.remove(hook);
763: }
764: }
765:
766: /**
767: * Shut down this kernel instance, unregistering the MBeans and releasing
768: * the MBeanServer.
769: */
770: public void shutdown() {
771: if (!running) {
772: return;
773: }
774: running = false;
775: log.debug("Starting kernel shutdown");
776:
777: notifyShutdownHooks();
778:
779: registry.stop();
780:
781: dependencyManager.close();
782: dependencyManager = null;
783:
784: synchronized (this ) {
785: notify();
786: }
787:
788: KernelRegistry.unregisterKernel(this );
789:
790: log.debug("Kernel shutdown complete");
791: }
792:
793: private void notifyShutdownHooks() {
794: while (!shutdownHooks.isEmpty()) {
795: Runnable hook;
796: synchronized (shutdownHooks) {
797: hook = (Runnable) shutdownHooks.removeFirst();
798: }
799: try {
800: hook.run();
801: } catch (Throwable e) {
802: log.warn("Error from kernel shutdown hook", e);
803: }
804: }
805: }
806:
807: public boolean isRunning() {
808: return running;
809: }
810:
811: public ClassLoader getClassLoaderFor(AbstractName name)
812: throws GBeanNotFoundException {
813: GBeanInstance gbeanInstance = registry.getGBeanInstance(name);
814: return gbeanInstance.getClassLoader();
815: }
816:
817: public ClassLoader getClassLoaderFor(String shortName)
818: throws GBeanNotFoundException {
819: return getClassLoaderFor(shortName, null);
820: }
821:
822: public ClassLoader getClassLoaderFor(Class type)
823: throws GBeanNotFoundException {
824: return getClassLoaderFor(null, type);
825: }
826:
827: public ClassLoader getClassLoaderFor(String shortName, Class type)
828: throws GBeanNotFoundException {
829: GBeanInstance gbeanInstance = registry.getGBeanInstance(
830: shortName, type);
831: return gbeanInstance.getClassLoader();
832: }
833:
834: /**
835: * @deprecated Experimental feature
836: */
837: public String getStateReason(AbstractName abstractName) {
838: try {
839: GBeanInstance gbeanInstance = registry
840: .getGBeanInstance(abstractName);
841: return gbeanInstance.getStateReason();
842: } catch (GBeanNotFoundException e) {
843: return null;
844: }
845: }
846: }
|