KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > common > AbstractComponent


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.ioc.common;
8
9 import org.jfox.ioc.Component;
10 import org.jfox.ioc.ComponentContext;
11 import org.jfox.ioc.annotation.Managable;
12 import org.jfox.ioc.ext.InitializableComponent;
13 import org.jfox.ioc.logger.Logger;
14
15 /**
16  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
17  */

18
19 public abstract class AbstractComponent implements Component, InitializableComponent {
20     /**
21      * system name of the system
22      */

23     protected String JavaDoc name = null;
24     /**
25      * the status value of the system
26      */

27     protected volatile State state = State.ORIGINAL;
28
29     protected transient Logger logger = Logger.getLogger(getClass().getName());
30     protected volatile long sequence = 0L;
31
32     protected transient Object JavaDoc proxyInstance = null;
33
34     protected transient ComponentContext context = null;
35
36     public AbstractComponent() {
37         setName(parseName(this.getClass().getName()));
38     }
39
40     public AbstractComponent(String JavaDoc name) {
41         setName(name);
42     }
43
44     public void setComponentContext(ComponentContext ctx) {
45         this.context = ctx;
46     }
47
48     public String JavaDoc getName() {
49         return name;
50     }
51
52     protected void setName(String JavaDoc name) {
53         this.name = name;
54     }
55
56     public State getState() {
57         return state;
58     }
59
60     public Logger getLogger() {
61         return logger;
62     }
63
64     protected void setLogger(Logger logger) {
65         this.logger = logger;
66     }
67
68     public boolean isInitialized() {
69         return state.equals(State.INITIALIZED)
70                || state.equals(State.STARTING)
71                || state.equals(State.STARTED)
72                || state.equals(State.STOPPING)
73                || state.equals(State.STOPPED);
74     }
75
76     @Managable
77     public void init() throws Exception JavaDoc {
78         if(!State.canInit(state)) {
79             logger.warn(name + " can not initialize, state = " + state);
80             return;
81         }
82
83 // if(logger == null) logger = Logger.getLogger(getClass().getName());
84
logger.info("initializing...");
85         state = State.INITIALIZING;
86
87         try {
88             doInit();
89         }
90         catch(Exception JavaDoc e) {
91             state = State.INTERRUPTED;
92             logger.error("initialze failed", e);
93         }
94
95         state = State.INITIALIZED;
96         logger.info("initialized.");
97
98     }
99
100     @Managable
101     public void destroy() throws Exception JavaDoc {
102         if(!State.canDestroy(state)) {
103             logger.warn(name + " can not destroy, state = " + state);
104             return;
105         }
106
107         logger.info("destroying...");
108         state = State.DESTROYING;
109         try {
110             doDestroy();
111         }
112         catch(Exception JavaDoc e) {
113             state = State.INTERRUPTED;
114             logger.error("stopping failed", e);
115             throw e;
116         }
117         state = State.DESTROYED;
118         logger.info("destroyed");
119
120     }
121
122     /**
123      * do actually create action
124      */

125     protected abstract void doInit() throws Exception JavaDoc;
126
127     /**
128      * do actually destory action
129      */

130     protected abstract void doDestroy() throws Exception JavaDoc;
131
132     // trip the ServiceName from it's class nameSpace
133
protected static String JavaDoc parseName(String JavaDoc className) {
134         int lastDotIndex = className.lastIndexOf(".");
135         return className.substring(lastDotIndex + 1);
136     }
137 }
138
139
Popular Tags