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 */ 017 package org.apache.geronimo.openejb; 018 019 import java.util.ArrayList; 020 import java.util.Collection; 021 import java.util.Hashtable; 022 import java.util.List; 023 import javax.management.ObjectName; 024 025 import org.apache.geronimo.j2ee.management.impl.InvalidObjectNameException; 026 import org.apache.geronimo.kernel.ObjectNameUtil; 027 import org.apache.geronimo.management.EJB; 028 import org.apache.geronimo.management.EJBModule; 029 import org.apache.geronimo.management.J2EEApplication; 030 import org.apache.geronimo.management.J2EEServer; 031 import org.apache.openejb.assembler.classic.EjbJarInfo; 032 import org.apache.openejb.UndeployException; 033 import org.apache.openejb.NoSuchApplicationException; 034 import org.apache.commons.logging.LogFactory; 035 import org.apache.commons.logging.Log; 036 037 /** 038 * @version $Revision: 451417 $ $Date: 2006-09-29 13:13:22 -0700 (Fri, 29 Sep 2006) $ 039 */ 040 public class EjbModuleImpl implements EJBModule { 041 042 private static final Log log = LogFactory.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, J2EEApplication application, String deploymentDescriptor, Collection<? extends EJB> ejbs, ClassLoader classLoader, OpenEjbSystem openEjbSystem, EjbJarInfo ejbJarInfo) { 054 this.objectName = objectName; 055 ObjectName myObjectName = ObjectNameUtil.getObjectName(objectName); 056 verifyObjectName(myObjectName); 057 058 this.server = server; 059 this.application = application; 060 this.deploymentDescriptor = deploymentDescriptor; 061 this.ejbs = ejbs; 062 063 this.classLoader = classLoader; 064 065 this.openEjbSystem = openEjbSystem; 066 this.ejbJarInfo = ejbJarInfo; 067 } 068 069 public String getObjectName() { 070 return objectName; 071 } 072 073 public boolean isStateManageable() { 074 return true; 075 } 076 077 public boolean isStatisticsProvider() { 078 return false; 079 } 080 081 public boolean isEventProvider() { 082 return true; 083 } 084 085 public String getDeploymentDescriptor() { 086 return deploymentDescriptor; 087 } 088 089 public String getServer() { 090 return server.getObjectName(); 091 } 092 093 public String getApplication() { 094 if (application == null) { 095 return null; 096 } 097 return application.getObjectName(); 098 } 099 100 public String[] getJavaVMs() { 101 return server.getJavaVMs(); 102 } 103 104 public String[] getEjbs() { 105 if (ejbs == null) { 106 return new String[0]; 107 } 108 109 ArrayList<EJB> copy; 110 synchronized (ejbs) { 111 copy = new ArrayList<EJB>(ejbs); 112 } 113 114 String[] result = new String[copy.size()]; 115 for (int i = 0; i < result.length; i++) { 116 result[i] = (copy.get(i)).getObjectName(); 117 } 118 return result; 119 } 120 121 122 protected void start() throws Exception { 123 openEjbSystem.createEjbJar(ejbJarInfo, classLoader); 124 } 125 126 protected void stop() { 127 try { 128 openEjbSystem.removeEjbJar(ejbJarInfo, classLoader); 129 } catch (NoSuchApplicationException e) { 130 log.error("Module does not exist.", e); 131 } catch (UndeployException e) { 132 List<Throwable> causes = e.getCauses(); 133 log.error(e.getMessage()+": Encountered "+causes.size()+" failures."); 134 for (Throwable throwable : causes) { 135 log.info(throwable); 136 } 137 } 138 } 139 140 /** 141 * ObjectName must match this pattern: 142 * <p/> 143 * domain:j2eeType=EJBModule,name=MyName,J2EEServer=MyServer,J2EEApplication=MyApplication 144 */ 145 private void verifyObjectName(ObjectName objectName) { 146 if (objectName.isPattern()) { 147 throw new InvalidObjectNameException("ObjectName can not be a pattern", objectName); 148 } 149 Hashtable keyPropertyList = objectName.getKeyPropertyList(); 150 if (!"EJBModule".equals(keyPropertyList.get("j2eeType"))) { 151 throw new InvalidObjectNameException("EJBModule object name j2eeType property must be 'EJBModule'", objectName); 152 } 153 if (!keyPropertyList.containsKey("name")) { 154 throw new InvalidObjectNameException("EJBModule object must contain a name property", objectName); 155 } 156 if (!keyPropertyList.containsKey("J2EEServer")) { 157 throw new InvalidObjectNameException("EJBModule object name must contain a J2EEServer property", objectName); 158 } 159 if (!keyPropertyList.containsKey("J2EEApplication")) { 160 throw new InvalidObjectNameException("EJBModule object name must contain a J2EEApplication property", objectName); 161 } 162 if (keyPropertyList.size() != 4) { 163 throw new InvalidObjectNameException("EJBModule object name can only have j2eeType, name, J2EEApplication, and J2EEServer properties", objectName); 164 } 165 } 166 }