001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.deployment;
023:
024: import java.util.Properties;
025:
026: import javax.management.ObjectName;
027: import javax.naming.InitialContext;
028:
029: import org.jboss.deployment.DeploymentException;
030: import org.jboss.resource.metadata.AdminObjectMetaData;
031: import org.jboss.resource.metadata.ConnectorMetaData;
032: import org.jboss.system.ServiceMBeanSupport;
033: import org.jboss.util.naming.Util;
034:
035: /**
036: * An admin object deployment
037: *
038: * @author <a href="adrian@jboss.com">Adrian Brock</a>
039: * @version $Revision: 57189 $
040: */
041: public class AdminObject extends ServiceMBeanSupport implements
042: AdminObjectMBean {
043: /** The resource adapter name */
044: protected ObjectName rarName;
045:
046: /** The admin object type */
047: protected String type;
048:
049: /** The properties */
050: protected Properties properties;
051:
052: /** The jndi name */
053: protected String jndiName;
054:
055: public String getJNDIName() {
056: return jndiName;
057: }
058:
059: public void setJNDIName(String jndiName) {
060: this .jndiName = jndiName;
061: }
062:
063: public Properties getProperties() {
064: return properties;
065: }
066:
067: public void setProperties(Properties properties) {
068: this .properties = properties;
069: }
070:
071: public ObjectName getRARName() {
072: return rarName;
073: }
074:
075: public void setRARName(ObjectName rarName) {
076: this .rarName = rarName;
077: }
078:
079: public String getType() {
080: return type;
081: }
082:
083: public void setType(String type) {
084: this .type = type;
085: }
086:
087: protected void startService() throws Exception {
088: AdminObjectMetaData aomd = retrieveAdminObjectMetaData();
089: if (aomd == null)
090: throw new DeploymentException(
091: "No admin object metadata type=" + type + " ra="
092: + rarName);
093:
094: Object adminObject = createAdminObject(aomd);
095:
096: bind(adminObject);
097: }
098:
099: protected void stopService() throws Exception {
100: unbind();
101: }
102:
103: /**
104: * Retrieve the admin object metadata
105: *
106: * @return the admin object metadata
107: * @throws DeploymentException for any error
108: */
109: protected AdminObjectMetaData retrieveAdminObjectMetaData()
110: throws DeploymentException {
111: try {
112: ConnectorMetaData cmd = (ConnectorMetaData) server
113: .getAttribute(rarName, "MetaData");
114: return cmd.getAdminObject(type);
115: } catch (Throwable t) {
116: DeploymentException.rethrowAsDeploymentException(
117: "Error retrieving admin object metadata type="
118: + type + " ra=" + rarName, t);
119: return null; // unreachable
120: }
121: }
122:
123: /**
124: * Create the admin object
125: *
126: * @param aomd the admin object metadata
127: * @return the admin object
128: * @throws DeploymentException for any error
129: */
130: protected Object createAdminObject(AdminObjectMetaData aomd)
131: throws DeploymentException {
132: try {
133: return AdminObjectFactory.createAdminObject(jndiName,
134: rarName, aomd, properties);
135: } catch (Throwable t) {
136: DeploymentException.rethrowAsDeploymentException(
137: "Error creating admin object metadata type=" + type
138: + " ra=" + rarName, t);
139: return null; // unreachable
140: }
141: }
142:
143: /**
144: * Bind the object into jndi
145: *
146: * @param object the object to bind
147: * @throws Exception for any error
148: */
149: protected void bind(Object object) throws Exception {
150: InitialContext ctx = new InitialContext();
151: try {
152: Util.bind(ctx, jndiName, object);
153: log.info("Bound admin object '"
154: + object.getClass().getName() + "' at '" + jndiName
155: + "'");
156: } finally {
157: ctx.close();
158: }
159: }
160:
161: /**
162: * Unbind the object from jndi
163: *
164: * @throws Exception for any error
165: */
166: protected void unbind() throws Exception {
167: InitialContext ctx = new InitialContext();
168: try {
169: Util.unbind(ctx, jndiName);
170: log.info("Unbound admin object at '" + jndiName + "'");
171: } finally {
172: ctx.close();
173: }
174: }
175: }
|