KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > kernel > deployment > jboss > beans > dependspojo > SimpleService


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * 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.test.kernel.deployment.jboss.beans.dependspojo;
23
24 import java.io.Serializable JavaDoc;
25
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28
29 import org.jboss.naming.Util;
30
31 /**
32  * Simple service to examine if dependencies are satisfied
33  *
34  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
35  * @version $Revision: 37406 $
36  */

37 public class SimpleService implements Serializable JavaDoc
38 {
39    /** The serialVersionUID */
40    private static final long serialVersionUID = 1L;
41
42    public static final String JavaDoc BASE_JNDI_NAME = "test/kernel/deployment/depends/pojo";
43    
44    public static final String JavaDoc CONSTRUCTED = "CONSTRUCTED";
45    public static final String JavaDoc CREATED = "CREATED";
46    public static final String JavaDoc STARTED = "STARTED";
47    public static final String JavaDoc STOPPED = "STOPPED";
48    public static final String JavaDoc DESTROYED = "DESTROYED";
49    public static final String JavaDoc FAILED = "FAILED";
50    
51    // Protected -----------------------------------------------------
52

53    protected transient InitialContext JavaDoc ctx;
54    
55    protected String JavaDoc name;
56    protected String JavaDoc[] depends;
57    protected String JavaDoc state = CONSTRUCTED;
58    
59    // Constructor ---------------------------------------------------
60

61    public SimpleService(String JavaDoc name) throws NamingException JavaDoc
62    {
63       // cache it
64
ctx = new InitialContext JavaDoc();
65       
66       this.name = name;
67   
68       log("CTOR");
69    }
70    
71    // Accessors -----------------------------------------------------
72

73    public String JavaDoc getName()
74    {
75       return name;
76    }
77       
78    public String JavaDoc[] getDepends()
79    {
80       return depends;
81    }
82    
83    public String JavaDoc getState()
84    {
85       return state;
86    }
87    
88    public void setDepends(String JavaDoc[] depends) throws NamingException JavaDoc
89    {
90       log("setDepends(" + depends + ")");
91       this.depends = depends;
92       
93       if (depends == null)
94       {
95          Util.unbind(ctx, getJndiName(name));
96          log("Destroyed binding: " + name);
97       }
98       else
99       {
100          Util.rebind(ctx, getJndiName(name), this);
101          log("Created binding: " + name);
102       }
103    }
104    
105    // Lifecycle -----------------------------------------------------
106

107    public void create() throws Exception JavaDoc
108    {
109       log("create()");
110       state = CREATED;
111       checkDependencies(CREATED);
112    }
113    
114    public void start() throws Exception JavaDoc
115    {
116       log("start()");
117       state = STARTED;
118       checkDependencies(STARTED);
119    }
120    
121    public void stop() throws Exception JavaDoc
122    {
123       log("stop()");
124       state = STOPPED;
125       checkDependencies(STARTED);
126    }
127    
128    public void destroy() throws Exception JavaDoc
129    {
130       log("destroy()");
131       state = DESTROYED;
132       checkDependencies(STOPPED);
133    }
134    
135    // protected -----------------------------------------------------
136

137    protected void log(Object JavaDoc message)
138    {
139       System.out.println(getName() + " - " + message);
140    }
141    
142    protected String JavaDoc getJndiName(String JavaDoc name)
143    {
144       return BASE_JNDI_NAME + '/' + name;
145    }
146    
147    protected void checkDependencies(String JavaDoc desiredState)
148    {
149       if (depends != null)
150       {
151          for (int i = 0; i < depends.length; i++)
152          {
153             try
154             {
155                SimpleService other = (SimpleService)ctx.lookup(getJndiName(depends[i]));
156                if (other.getState().equals(desiredState))
157                {
158                   log(depends[i] + " is " + other.getState());
159                }
160                else
161                {
162                   throw new IllegalStateException JavaDoc(depends[i] + " not " + desiredState);
163                }
164             }
165             catch (NamingException JavaDoc e)
166             {
167                throw new RuntimeException JavaDoc(depends[i] + " not bound\n");
168             }
169          }
170       }
171    }
172    
173    // Overrides -----------------------------------------------------
174

175    public String JavaDoc toString()
176    {
177       StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
178       sbuf
179       .append(getClass().getName())
180       .append("[ name=").append(name)
181       .append(", state=").append(state);
182       if (depends != null)
183       {
184          sbuf.append(", depends=[");
185          for (int i = 0; i < depends.length; i++)
186             sbuf.append(' ').append(depends[i]);
187          sbuf.append(" ]");
188       }
189       sbuf.append(" ]");
190       
191       return sbuf.toString();
192    }
193 }
194
Popular Tags