001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.openejb;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Hashtable;
021: import java.util.List;
022: import javax.management.ObjectName;
023:
024: import org.apache.geronimo.j2ee.management.impl.InvalidObjectNameException;
025: import org.apache.geronimo.kernel.ObjectNameUtil;
026: import org.apache.geronimo.management.EJB;
027: import org.apache.geronimo.management.EJBModule;
028: import org.apache.geronimo.management.J2EEApplication;
029: import org.apache.geronimo.management.J2EEServer;
030: import org.apache.openejb.assembler.classic.EjbJarInfo;
031: import org.apache.openejb.UndeployException;
032: import org.apache.openejb.NoSuchApplicationException;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.commons.logging.Log;
035:
036: /**
037: * @version $Revision: 583409 $ $Date: 2007-10-10 02:38:51 -0700 (Wed, 10 Oct 2007) $
038: */
039: public class EjbModuleImpl implements EJBModule {
040:
041: private static final Log log = LogFactory
042: .getLog(EjbModuleImpl.class);
043: private final J2EEServer server;
044: private final J2EEApplication application;
045: private final String deploymentDescriptor;
046: private final String objectName;
047: private final Collection<? extends EJB> ejbs;
048: private final ClassLoader classLoader;
049:
050: private final OpenEjbSystem openEjbSystem;
051: private final EjbJarInfo ejbJarInfo;
052:
053: public EjbModuleImpl(String objectName, J2EEServer server,
054: J2EEApplication application, String deploymentDescriptor,
055: Collection<? extends EJB> ejbs, ClassLoader classLoader,
056: OpenEjbSystem openEjbSystem, EjbJarInfo ejbJarInfo) {
057: this .objectName = objectName;
058: ObjectName myObjectName = ObjectNameUtil
059: .getObjectName(objectName);
060: verifyObjectName(myObjectName);
061:
062: this .server = server;
063: this .application = application;
064: this .deploymentDescriptor = deploymentDescriptor;
065: this .ejbs = ejbs;
066:
067: this .classLoader = classLoader;
068:
069: this .openEjbSystem = openEjbSystem;
070: this .ejbJarInfo = ejbJarInfo;
071: }
072:
073: public String getObjectName() {
074: return objectName;
075: }
076:
077: public boolean isStateManageable() {
078: return true;
079: }
080:
081: public boolean isStatisticsProvider() {
082: return false;
083: }
084:
085: public boolean isEventProvider() {
086: return true;
087: }
088:
089: public String getDeploymentDescriptor() {
090: return deploymentDescriptor;
091: }
092:
093: public String getServer() {
094: return server.getObjectName();
095: }
096:
097: public String getApplication() {
098: if (application == null) {
099: return null;
100: }
101: return application.getObjectName();
102: }
103:
104: public String[] getJavaVMs() {
105: return server.getJavaVMs();
106: }
107:
108: public String[] getEjbs() {
109: if (ejbs == null) {
110: return new String[0];
111: }
112:
113: ArrayList<EJB> copy;
114: synchronized (ejbs) {
115: copy = new ArrayList<EJB>(ejbs);
116: }
117:
118: String[] result = new String[copy.size()];
119: for (int i = 0; i < result.length; i++) {
120: result[i] = (copy.get(i)).getObjectName();
121: }
122: return result;
123: }
124:
125: protected void start() throws Exception {
126: openEjbSystem.createEjbJar(ejbJarInfo, classLoader);
127: }
128:
129: protected void stop() {
130: try {
131: openEjbSystem.removeEjbJar(ejbJarInfo, classLoader);
132: } catch (NoSuchApplicationException e) {
133: log.error("Module does not exist.", e);
134: } catch (UndeployException e) {
135: List<Throwable> causes = e.getCauses();
136: log.error(e.getMessage() + ": Encountered " + causes.size()
137: + " failures.");
138: for (Throwable throwable : causes) {
139: log.info(throwable);
140: }
141: }
142: }
143:
144: /**
145: * ObjectName must match this pattern:
146: * <p/>
147: * domain:j2eeType=EJBModule,name=MyName,J2EEServer=MyServer,J2EEApplication=MyApplication
148: */
149: private void verifyObjectName(ObjectName objectName) {
150: if (objectName.isPattern()) {
151: throw new InvalidObjectNameException(
152: "ObjectName can not be a pattern", objectName);
153: }
154: Hashtable keyPropertyList = objectName.getKeyPropertyList();
155: if (!"EJBModule".equals(keyPropertyList.get("j2eeType"))) {
156: throw new InvalidObjectNameException(
157: "EJBModule object name j2eeType property must be 'EJBModule'",
158: objectName);
159: }
160: if (!keyPropertyList.containsKey("name")) {
161: throw new InvalidObjectNameException(
162: "EJBModule object must contain a name property",
163: objectName);
164: }
165: if (!keyPropertyList.containsKey("J2EEServer")) {
166: throw new InvalidObjectNameException(
167: "EJBModule object name must contain a J2EEServer property",
168: objectName);
169: }
170: if (!keyPropertyList.containsKey("J2EEApplication")) {
171: throw new InvalidObjectNameException(
172: "EJBModule object name must contain a J2EEApplication property",
173: objectName);
174: }
175: if (keyPropertyList.size() != 4) {
176: throw new InvalidObjectNameException(
177: "EJBModule object name can only have j2eeType, name, J2EEApplication, and J2EEServer properties",
178: objectName);
179: }
180: }
181: }
|