1 /******************************************************************************
2 * Copyright (C) PicoContainer Organization. All rights reserved. *
3 * ------------------------------------------------------------------------- *
4 * The software in this package is published under the terms of the BSD *
5 * style license a copy of which has been included with this distribution in *
6 * the license.html file. *
7 * *
8 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
9 *****************************************************************************/
10
11 package org.picocontainer.extras;
12
13 import org.picocontainer.PicoContainer;
14 import org.picocontainer.lifecycle.LifecyclePicoAdaptor;
15 import org.picocontainer.lifecycle.Startable;
16 import org.picocontainer.lifecycle.Stoppable;
17 import org.picocontainer.lifecycle.Disposable;
18
19 public class DefaultLifecyclePicoAdaptor implements LifecyclePicoAdaptor {
20
21 private Startable startableAggregatedComponent;
22 private Stoppable stoppableAggregatedComponent;
23 private Disposable disposableAggregatedComponent;
24 private boolean started;
25 private boolean disposed;
26 private final PicoContainer picoContainer;
27
28 public DefaultLifecyclePicoAdaptor(PicoContainer picoContainer) {
29 this.picoContainer = picoContainer;
30 }
31
32 public boolean isStarted() {
33 return started;
34 }
35
36 public boolean isStopped() {
37 return !started;
38 }
39
40 public boolean isDisposed() {
41 return disposed;
42 }
43
44 private void initializeIfNotInitialized() {
45 if (startableAggregatedComponent == null) {
46 try {
47 startableAggregatedComponent = (Startable) picoContainer.getComponentMulticaster(true, false);
48 } catch (ClassCastException e) {
49 }
50 }
51 if (stoppableAggregatedComponent == null) {
52 try {
53
54 stoppableAggregatedComponent = (Stoppable) picoContainer.getComponentMulticaster(false, false);
55 } catch (ClassCastException e) {
56 }
57 }
58 if (disposableAggregatedComponent == null) {
59 try {
60 //TODO-Aslak broken ?
61 Object o = picoContainer.getComponentMulticaster(false, false);
62 disposableAggregatedComponent = (Disposable) o;
63 } catch (ClassCastException e) {
64 }
65 }
66
67 }
68
69 public void start() throws Exception {
70 checkDisposed();
71 initializeIfNotInitialized();
72 if (started) {
73 throw new IllegalStateException("Already started.");
74 }
75 started = true;
76 if (startableAggregatedComponent != null) {
77 startableAggregatedComponent.start();
78 }
79 }
80
81 public void stop() throws Exception {
82 checkDisposed();
83 initializeIfNotInitialized();
84 if (started == false) {
85 throw new IllegalStateException("Already stopped.");
86 }
87 started = false;
88 if (stoppableAggregatedComponent != null) {
89 stoppableAggregatedComponent.stop();
90 }
91 }
92
93 public void dispose() throws Exception {
94 checkDisposed();
95 initializeIfNotInitialized();
96 disposed = true;
97 if (disposableAggregatedComponent != null) {
98 disposableAggregatedComponent.dispose();
99 }
100 }
101
102 private void checkDisposed() {
103 if (disposed) {
104 throw new IllegalStateException("Components Disposed Of");
105 }
106 }
107
108 }
109
This page was automatically generated by Maven