KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > deployment > AdminObject


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.resource.deployment;
23
24 import java.util.Properties JavaDoc;
25
26 import javax.management.ObjectName JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28
29 import org.jboss.deployment.DeploymentException;
30 import org.jboss.resource.metadata.AdminObjectMetaData;
31 import org.jboss.resource.metadata.ConnectorMetaData;
32 import org.jboss.system.ServiceMBeanSupport;
33 import org.jboss.util.naming.Util;
34
35 /**
36  * An admin object deployment
37  *
38  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
39  * @version $Revision: 38341 $
40  */

41 public class AdminObject extends ServiceMBeanSupport implements AdminObjectMBean
42 {
43    /** The resource adapter name */
44    protected ObjectName JavaDoc rarName;
45
46    /** The admin object type */
47    protected String JavaDoc type;
48    
49    /** The properties */
50    protected Properties JavaDoc properties;
51    
52    /** The jndi name */
53    protected String JavaDoc jndiName;
54
55    public String JavaDoc getJNDIName()
56    {
57       return jndiName;
58    }
59
60    public void setJNDIName(String JavaDoc jndiName)
61    {
62       this.jndiName = jndiName;
63    }
64
65    public Properties JavaDoc getProperties()
66    {
67       return properties;
68    }
69
70    public void setProperties(Properties JavaDoc properties)
71    {
72       this.properties = properties;
73    }
74
75    public ObjectName JavaDoc getRARName()
76    {
77       return rarName;
78    }
79
80    public void setRARName(ObjectName JavaDoc rarName)
81    {
82       this.rarName = rarName;
83    }
84
85    public String JavaDoc getType()
86    {
87       return type;
88    }
89
90    public void setType(String JavaDoc type)
91    {
92       this.type = type;
93    }
94    
95    protected void startService() throws Exception JavaDoc
96    {
97       AdminObjectMetaData aomd = retrieveAdminObjectMetaData();
98       if (aomd == null)
99          throw new DeploymentException("No admin object metadata type=" + type + " ra=" + rarName);
100
101       Object JavaDoc adminObject = createAdminObject(aomd);
102       
103       bind(adminObject);
104    }
105    
106    protected void stopService() throws Exception JavaDoc
107    {
108       unbind();
109    }
110
111    /**
112     * Retrieve the admin object metadata
113     *
114     * @return the admin object metadata
115     * @throws DeploymentException for any error
116     */

117    protected AdminObjectMetaData retrieveAdminObjectMetaData() throws DeploymentException
118    {
119       try
120       {
121          ConnectorMetaData cmd = (ConnectorMetaData) server.getAttribute(rarName, "MetaData");
122          return cmd.getAdminObject(type);
123       }
124       catch (Throwable JavaDoc t)
125       {
126          DeploymentException.rethrowAsDeploymentException("Error retrieving admin object metadata type=" + type + " ra=" + rarName, t);
127          return null; // unreachable
128
}
129    }
130
131    /**
132     * Create the admin object
133     *
134     * @param aomd the admin object metadata
135     * @return the admin object
136     * @throws DeploymentException for any error
137     */

138    protected Object JavaDoc createAdminObject(AdminObjectMetaData aomd) throws DeploymentException
139    {
140       try
141       {
142          return AdminObjectFactory.createAdminObject(jndiName, rarName, aomd, properties);
143       }
144       catch (Throwable JavaDoc t)
145       {
146          DeploymentException.rethrowAsDeploymentException("Error creating admin object metadata type=" + type + " ra=" + rarName, t);
147          return null; // unreachable
148
}
149    }
150
151    /**
152     * Bind the object into jndi
153     *
154     * @param object the object to bind
155     * @throws Exception for any error
156     */

157    protected void bind(Object JavaDoc object) throws Exception JavaDoc
158    {
159       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
160       try
161       {
162          Util.bind(ctx, jndiName, object);
163          log.info("Bound admin object '" + object.getClass().getName() + "' at '" + jndiName + "'");
164       }
165       finally
166       {
167          ctx.close();
168       }
169    }
170
171    /**
172     * Unbind the object from jndi
173     *
174     * @throws Exception for any error
175     */

176    protected void unbind() throws Exception JavaDoc
177    {
178       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
179       try
180       {
181          Util.unbind(ctx, jndiName);
182          log.info("Unbound admin object at '" + jndiName + "'");
183       }
184       finally
185       {
186          ctx.close();
187       }
188    }
189 }
190
Popular Tags