1 45 package org.openejb.alt.config; 46 47 import java.lang.reflect.Method ; 48 49 import org.openejb.OpenEJBException; 50 import org.openejb.alt.config.ejb11.EjbDeployment; 51 import org.openejb.alt.config.ejb11.EjbJar; 52 import org.openejb.alt.config.ejb11.OpenejbJar; 53 import org.openejb.alt.config.ejb11.ResourceLink; 54 import org.openejb.alt.config.ejb11.ResourceRef; 55 import org.openejb.alt.config.sys.Connector; 56 import org.openejb.alt.config.sys.Container; 57 import org.openejb.alt.config.sys.Openejb; 58 import org.openejb.util.SafeToolkit; 59 60 85 public class AutoDeployer { 86 87 private Openejb config; 88 private String configFile; 89 private Container[] containers; 90 private Connector[] resources; 91 private ClassLoader classLoader; 92 private String jarLocation; 93 94 public AutoDeployer(Openejb config) { 95 this.config = config; 96 97 98 this.containers = config.getContainer(); 99 100 101 this.resources = config.getConnector(); 102 System.out.println("resources "+resources.length); 103 } 104 105 public void init() throws OpenEJBException { 106 } 107 108 public OpenejbJar deploy(EjbJarUtils ejbJarUtils, String jarLocation, ClassLoader classLoader) throws OpenEJBException { 109 this.jarLocation = jarLocation; 110 this.classLoader = classLoader; 111 OpenejbJar openejbJar = new OpenejbJar(); 112 113 Bean[] beans = ejbJarUtils.getBeans(); 114 115 for (int i = 0; i < beans.length; i++) { 116 openejbJar.addEjbDeployment(deployBean(beans[i], jarLocation)); 117 } 118 return openejbJar; 119 } 120 121 private EjbDeployment deployBean(Bean bean, String jarLocation) throws OpenEJBException { 122 EjbDeployment deployment = new EjbDeployment(); 123 124 deployment.setEjbName(bean.getEjbName()); 125 126 deployment.setDeploymentId(autoAssignDeploymentId(bean)); 127 128 deployment.setContainerId(autoAssignContainerId(bean)); 129 130 ResourceRef[] refs = bean.getResourceRef(); 131 132 if (refs.length > 1){ 133 throw new OpenEJBException("Beans with more that one resource-ref cannot be autodeployed; there is no accurate way to determine how the references should be mapped."); 134 } 135 136 for (int i = 0; i < refs.length; i++) { 137 deployment.addResourceLink(autoAssingResourceRef(refs[i])); 138 } 139 140 if (bean.getType().equals("CMP_ENTITY")){ 141 if (bean.getHome() != null){ 142 Class tempBean = loadClass(bean.getHome()); 143 if (hasFinderMethods(tempBean)){ 144 throw new OpenEJBException("CMP 1.1 Beans with finder methods cannot be autodeployed; finder methods require OQL Select statements which cannot be generated accurately."); 145 } 146 } 147 if (bean.getLocalHome() != null){ 148 Class tempBean = loadClass(bean.getLocalHome()); 149 if (hasFinderMethods(tempBean)){ 150 throw new OpenEJBException("CMP 1.1 Beans with finder methods cannot be autodeployed; finder methods require OQL Select statements which cannot be generated accurately."); 151 } 152 } 153 } 154 155 return deployment; 156 } 157 158 private Class loadClass(String className) throws OpenEJBException { 159 try { 160 return classLoader.loadClass(className); 161 } catch (ClassNotFoundException cnfe) { 162 throw new OpenEJBException(SafeToolkit.messages.format("cl0007", className, this.jarLocation)); 163 } 164 } 165 166 private boolean hasFinderMethods(Class bean) 167 throws OpenEJBException { 168 169 Method [] methods = bean.getMethods(); 170 171 for (int i = 0; i < methods.length; i++) { 172 if (methods[i].getName().startsWith("find") 173 && !methods[i].getName().equals("findByPrimaryKey")) { 174 return true; 175 } 176 } 177 return false; 178 } 179 180 private String autoAssignDeploymentId(Bean bean) throws OpenEJBException { 181 return bean.getEjbName(); 182 } 183 184 private String autoAssignContainerId(Bean bean) throws OpenEJBException { 185 String answer = null; 186 boolean replied = false; 187 188 Container[] cs = getUsableContainers(bean); 189 190 if (cs.length == 0) { 191 throw new OpenEJBException("A container of type "+bean.getType()+" must be declared in the configuration file."); 192 } 193 return cs[0].getId(); 194 } 195 196 private ResourceLink autoAssingResourceRef(ResourceRef ref) throws OpenEJBException { 197 if (resources.length == 0) { 198 throw new OpenEJBException("A Connector must be declared in the configuration file to satisfy the resource-ref "+ref.getResRefName()); 199 } 200 201 ResourceLink link = new ResourceLink(); 202 link.setResRefName(ref.getResRefName()); 203 link.setResId(resources[0].getId()); 204 return link; 205 } 206 207 private Container[] getUsableContainers(Bean bean) { 208 return EjbJarUtils.getUsableContainers(containers, bean); 209 } 210 } 211 | Popular Tags |