Home » geronimo-2.2-source-release » org.apache.geronimo.tomcat.deployment » [javadoc | source]

    1   /**
    2    *
    3    * Copyright 2003-2004 The Apache Software Foundation
    4    *
    5    *  Licensed under the Apache License, Version 2.0 (the "License");
    6    *  you may not use this file except in compliance with the License.
    7    *  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.geronimo.tomcat.deployment;
   18   
   19   import java.io.ByteArrayOutputStream;
   20   import java.io.File;
   21   import java.io.IOException;
   22   import java.io.ObjectOutputStream;
   23   import java.net.URI;
   24   import java.net.URL;
   25   import java.security.PermissionCollection;
   26   import java.security.Permissions;
   27   import java.util.Collection;
   28   import java.util.Collections;
   29   import java.util.HashMap;
   30   import java.util.HashSet;
   31   import java.util.Iterator;
   32   import java.util.List;
   33   import java.util.Map;
   34   import java.util.Set;
   35   
   36   import javax.management.MalformedObjectNameException;
   37   import javax.management.ObjectName;
   38   import javax.naming.Reference;
   39   import javax.xml.namespace.QName;
   40   
   41   import junit.framework.TestCase;
   42   
   43   import org.apache.commons.io.FileUtils;
   44   import org.apache.geronimo.common.DeploymentException;
   45   import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTrackingCoordinator;
   46   import org.apache.geronimo.deployment.DeploymentContext;
   47   import org.apache.geronimo.deployment.util.UnpackedJarFile;
   48   import org.apache.geronimo.gbean.GBeanData;
   49   import org.apache.geronimo.gbean.GBeanInfo;
   50   import org.apache.geronimo.gbean.GBeanInfoBuilder;
   51   import org.apache.geronimo.j2ee.deployment.EARContext;
   52   import org.apache.geronimo.j2ee.deployment.EJBReferenceBuilder;
   53   import org.apache.geronimo.j2ee.deployment.Module;
   54   import org.apache.geronimo.j2ee.deployment.RefContext;
   55   import org.apache.geronimo.j2ee.deployment.ResourceReferenceBuilder;
   56   import org.apache.geronimo.j2ee.deployment.ServiceReferenceBuilder;
   57   import org.apache.geronimo.j2ee.j2eeobjectnames.J2eeContext;
   58   import org.apache.geronimo.j2ee.j2eeobjectnames.J2eeContextImpl;
   59   import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
   60   import org.apache.geronimo.j2ee.management.impl.J2EEServerImpl;
   61   import org.apache.geronimo.kernel.KernelFactory;
   62   import org.apache.geronimo.kernel.Kernel;
   63   import org.apache.geronimo.kernel.config.ConfigurationManagerImpl;
   64   import org.apache.geronimo.kernel.config.Configuration;
   65   import org.apache.geronimo.kernel.config.ConfigurationModuleType;
   66   import org.apache.geronimo.kernel.config.ConfigurationStore;
   67   import org.apache.geronimo.kernel.config.InvalidConfigException;
   68   import org.apache.geronimo.kernel.config.NoSuchConfigException;
   69   import org.apache.geronimo.kernel.config.ConfigurationData;
   70   import org.apache.geronimo.kernel.config.ConfigurationManager;
   71   import org.apache.geronimo.kernel.jmx.JMXUtil;
   72   import org.apache.geronimo.kernel.management.State;
   73   import org.apache.geronimo.security.SecurityServiceImpl;
   74   import org.apache.geronimo.security.jacc.ApplicationPolicyConfigurationManager;
   75   import org.apache.geronimo.security.jacc.ComponentPermissions;
   76   import org.apache.geronimo.system.serverinfo.ServerInfo;
   77   import org.apache.geronimo.tomcat.ConnectorGBean;
   78   import org.apache.geronimo.tomcat.EngineGBean;
   79   import org.apache.geronimo.tomcat.HostGBean;
   80   import org.apache.geronimo.tomcat.RealmGBean;
   81   import org.apache.geronimo.tomcat.TomcatContainer;
   82   import org.apache.geronimo.transaction.context.TransactionContextManager;
   83   import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
   84   
   85   /**
   86    * @version $Rev: 159325 $ $Date: 2005-03-28 15:53:03 -0700 (Mon, 28 Mar 2005) $
   87    */
   88   public class TomcatModuleBuilderTest extends TestCase {
   89   
   90       protected Kernel kernel;
   91   
   92       private GBeanData container;
   93   
   94       private ObjectName containerName;
   95   
   96       private ObjectName connectorName;
   97   
   98       private GBeanData connector;
   99   
  100       private ObjectName engineName;
  101   
  102       private GBeanData engine;
  103   
  104       private ObjectName hostName;
  105   
  106       private GBeanData host;
  107   
  108       private ObjectName realmName;
  109   
  110       private GBeanData realm;
  111   
  112       private GBeanData securityServiceGBean;
  113   
  114       private ObjectName securityServiceName;
  115   
  116       private ObjectName serverInfoName;
  117   
  118       private GBeanData serverInfoGBean;
  119   
  120       private ObjectName tmName;
  121   
  122       private ObjectName ctcName;
  123   
  124       private GBeanData tm;
  125   
  126       private GBeanData ctc;
  127   
  128       private ObjectName tcmName;
  129   
  130       private GBeanData tcm;
  131   
  132       private ClassLoader cl;
  133   
  134       private J2eeContext moduleContext = new J2eeContextImpl("tomcat.test",
  135               "test", "null", NameFactory.WEB_MODULE, "Test", null, null);
  136   
  137       private TomcatModuleBuilder builder;
  138   
  139       private File basedir = new File(System.getProperty("basedir", "."));
  140   
  141       private URI parentId = URI.create("org/apache/geronimo/Foo");
  142   
  143       public void testDeployWar4() throws Exception {
  144           File outputPath = new File(basedir,
  145                   "target/test-resources/deployables/war4");
  146           recursiveDelete(outputPath);
  147           outputPath.mkdirs();
  148           File path = new File(basedir, "src/test-resources/deployables/war4");
  149           File dest = new File(basedir, "target/test-resources/deployables/war4/war");
  150           recursiveCopy(path, dest);
  151           UnpackedJarFile jarFile = new UnpackedJarFile(path);
  152           Module module = builder.createModule(null, jarFile);
  153           URI id = new URI("war4");
  154           
  155           ObjectName jaccBeanName = NameFactory.getComponentName(null, null, null, null, "foo", NameFactory.JACC_MANAGER, moduleContext);
  156           GBeanData jaccBeanData = new GBeanData(jaccBeanName, ApplicationPolicyConfigurationManager.GBEAN_INFO);
  157           PermissionCollection excludedPermissions= new Permissions();
  158           PermissionCollection uncheckedPermissions= new Permissions();
  159           ComponentPermissions componentPermissions = new ComponentPermissions(excludedPermissions, uncheckedPermissions, new HashMap());
  160           Map contextIDToPermissionsMap = new HashMap();
  161           contextIDToPermissionsMap.put("test_J2EEApplication=null_J2EEServer=bar_j2eeType=WebModule_name=org/apache/geronimo/test", componentPermissions);
  162           jaccBeanData.setAttribute("contextIdToPermissionsMap", contextIDToPermissionsMap);
  163           jaccBeanData.setAttribute("principalRoleMap", new HashMap());
  164           jaccBeanData.setAttribute("roleDesignates", new HashMap());
  165           start(jaccBeanData);        
  166           
  167           EARContext earContext = createEARContext(outputPath, id);
  168           earContext.setJaccManagerName(jaccBeanName);
  169           ObjectName serverName = earContext.getServerObjectName();
  170           GBeanData server = new GBeanData(serverName, J2EEServerImpl.GBEAN_INFO);
  171           start(server);
  172           builder.initContext(earContext, module, cl);
  173           builder.addGBeans(earContext, module, cl);
  174           earContext.close();
  175           module.close();
  176           GBeanData configData = earContext.getConfigurationGBeanData();
  177           configData.setAttribute("baseURL", path.toURL());
  178           kernel.loadGBean(configData, cl);
  179   
  180           kernel.startRecursiveGBean(configData.getName());
  181           if (kernel.getGBeanState(configData.getName()) != State.RUNNING_INDEX) {
  182               fail("gbean not started: " + configData.getName());
  183           }
  184   
  185           assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(ObjectName.getInstance("test:J2EEApplication=null,J2EEServer=bar,j2eeType=WebModule,name=org/apache/geronimo/test")));
  186   
  187           Set names = kernel.listGBeans(ObjectName.getInstance("test:J2EEApplication=null,J2EEServer=bar,*"));
  188           System.out.println("Object names: " + names);
  189           for (Iterator iterator = names.iterator(); iterator.hasNext();) {
  190               ObjectName objectName = (ObjectName) iterator.next();
  191               System.out.println("STATE: " + kernel.getGBeanState(objectName) + " - " + objectName.getCanonicalName());
  192               assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(objectName));
  193           }
  194   
  195           //If we got here with no errors, then Tomcat deployed the war and loaded the classes
  196           
  197           kernel.stopGBean(configData.getName());
  198           kernel.unloadGBean(configData.getName());
  199   
  200           kernel.loadGBean(configData, cl);
  201           kernel.startRecursiveGBean(configData.getName());
  202           kernel.stopGBean(configData.getName());
  203           kernel.unloadGBean(configData.getName());
  204       }
  205   
  206       private EARContext createEARContext(File outputPath, URI id)
  207               throws MalformedObjectNameException, DeploymentException {
  208           EARContext earContext = new EARContext(outputPath, id,
  209                   ConfigurationModuleType.WAR, parentId, kernel, moduleContext
  210                           .getJ2eeApplicationName(), tcmName, ctcName, null,
  211                   null, null, new RefContext(new EJBReferenceBuilder() {
  212   
  213                       public Reference createEJBLocalReference(String objectName,
  214                               boolean isSession, String localHome, String local)
  215                               throws DeploymentException {
  216                           return null;
  217                       }
  218   
  219                       public Reference createEJBRemoteReference(
  220                               String objectName, boolean isSession, String home,
  221                               String remote) throws DeploymentException {
  222                           return null;
  223                       }
  224   
  225                       public Reference createCORBAReference(URI corbaURL,
  226                               String objectName, ObjectName containerName,
  227                               String home) throws DeploymentException {
  228                           return null;
  229                       }
  230   
  231                       public Object createHandleDelegateReference() {
  232                           return null;
  233                       }
  234                   }, new ResourceReferenceBuilder() {
  235   
  236                       public Reference createResourceRef(String containerId,
  237                               Class iface) throws DeploymentException {
  238                           return null;
  239                       }
  240   
  241                       public Reference createAdminObjectRef(String containerId,
  242                               Class iface) throws DeploymentException {
  243                           return null;
  244                       }
  245   
  246                       public ObjectName locateResourceName(ObjectName query)
  247                               throws DeploymentException {
  248                           return null;
  249                       }
  250   
  251                       public GBeanData locateActivationSpecInfo(
  252                               ObjectName resourceAdapterName,
  253                               String messageListenerInterface)
  254                               throws DeploymentException {
  255                           return null;
  256                       }
  257   
  258                       public GBeanData locateResourceAdapterGBeanData(
  259                               ObjectName resourceAdapterModuleName)
  260                               throws DeploymentException {
  261                           return null;
  262                       }
  263   
  264                       public GBeanData locateAdminObjectInfo(
  265                               ObjectName resourceAdapterModuleName,
  266                               String adminObjectInterfaceName)
  267                               throws DeploymentException {
  268                           return null;
  269                       }
  270   
  271                       public GBeanData locateConnectionFactoryInfo(
  272                               ObjectName resourceAdapterModuleName,
  273                               String connectionFactoryInterfaceName)
  274                               throws DeploymentException {
  275                           return null;
  276                       }
  277                   }, new ServiceReferenceBuilder() {
  278                       // it could return a Service or a Reference, we don't care
  279                       public Object createService(Class serviceInterface,
  280                               URI wsdlURI, URI jaxrpcMappingURI,
  281                               QName serviceQName, Map portComponentRefMap,
  282                               List handlerInfos, Map portLocationMap,
  283                               Map credentialsNameMap,
  284                               DeploymentContext deploymentContext, Module module,
  285                               ClassLoader classLoader) throws DeploymentException {
  286                           return null;
  287                       }
  288                   }, kernel));
  289           return earContext;
  290       }
  291   
  292       private void recursiveDelete(File path) {
  293           // does not delete top level dir passed in
  294           File[] listing = path.listFiles();
  295           for (int i = 0; i < ((listing == null) ? 0 : listing.length); i++) {
  296               File file = listing[i];
  297               if (file.isDirectory()) {
  298                   recursiveDelete(file);
  299               }
  300               file.delete();
  301           }
  302       }
  303   
  304       public void recursiveCopy(File src, File dest) throws IOException {       
  305           Collection files = FileUtils.listFiles(src,null,true);
  306           Iterator iterator = files.iterator();
  307           while(iterator.hasNext()){
  308               File file = (File) iterator.next(); 
  309               if (file.getAbsolutePath().indexOf(".svn") < 0){
  310                   String pathToFile = file.getPath();
  311                   String relativePath = pathToFile.substring(src.getPath().length(), pathToFile.length());
  312                   FileUtils.copyFileToDirectory(file,new File(dest.getPath() + relativePath));
  313               }
  314           }
  315       }
  316   
  317       protected void setUp() throws Exception {
  318           cl = this.getClass().getClassLoader();
  319           containerName = NameFactory.getWebComponentName(null, null, null, null,
  320                   "tomcatContainer", "WebResource", moduleContext);
  321           connectorName = NameFactory.getWebComponentName(null, null, null, null,
  322                   "tomcatConnector", "WebResource", moduleContext);
  323           realmName = NameFactory.getWebComponentName(null, null, null, null,
  324                   "tomcatRealm", "WebResource", moduleContext);
  325           engineName = NameFactory.getWebComponentName(null, null, null, null,
  326                   "tomcatEngine", "WebResource", moduleContext);
  327           hostName = NameFactory.getWebComponentName(null, null, null, null,
  328                   "tomcatHost", "WebResource", moduleContext);
  329   
  330           tmName = NameFactory.getComponentName(null, null, null, null, null,
  331                   "TransactionManager", NameFactory.JTA_RESOURCE, moduleContext);
  332           tcmName = NameFactory.getComponentName(null, null, null, null, null,
  333                   "TransactionContextManager", NameFactory.JTA_RESOURCE,
  334                   moduleContext);
  335           ctcName = new ObjectName(
  336                   "geronimo.server:role=ConnectionTrackingCoordinator");
  337   
  338           kernel = KernelFactory.newInstance().createKernel("foo");
  339           kernel.boot();
  340   
  341           GBeanData store = new GBeanData(JMXUtil
  342                   .getObjectName("foo:j2eeType=ConfigurationStore,name=mock"),
  343                   MockConfigStore.GBEAN_INFO);
  344           kernel.loadGBean(store, this.getClass().getClassLoader());
  345           kernel.startGBean(store.getName());
  346   
  347           ObjectName configurationManagerName = new ObjectName(":j2eeType=ConfigurationManager,name=Basic");
  348           GBeanData configurationManagerData = new GBeanData(configurationManagerName, ConfigurationManagerImpl.GBEAN_INFO);
  349           configurationManagerData.setReferencePatterns("Stores", Collections.singleton(store.getName()));
  350           kernel.loadGBean(configurationManagerData, getClass().getClassLoader());
  351           kernel.startGBean(configurationManagerName);
  352           ConfigurationManager configurationManager = (ConfigurationManager) kernel.getProxyManager().createProxy(configurationManagerName, ConfigurationManager.class);
  353   
  354           ObjectName baseConfigName = configurationManager.load(parentId);
  355           kernel.startGBean(baseConfigName);
  356   
  357           serverInfoName = new ObjectName("geronimo.system:name=ServerInfo");
  358           serverInfoGBean = new GBeanData(serverInfoName, ServerInfo.GBEAN_INFO);
  359           serverInfoGBean.setAttribute("baseDirectory", ".");
  360           start(serverInfoGBean);
  361   
  362           // install the policy configuration factory
  363           securityServiceName = new ObjectName("foo:j2eeType=SecurityService");
  364           securityServiceGBean = new GBeanData(securityServiceName,
  365                   SecurityServiceImpl.GBEAN_INFO);
  366           securityServiceGBean.setReferencePattern("ServerInfo", serverInfoName);
  367           securityServiceGBean
  368                   .setAttribute("policyConfigurationFactory",
  369                           "org.apache.geronimo.security.jacc.GeronimoPolicyConfigurationFactory");
  370           securityServiceGBean.setAttribute("policyProvider",
  371                   "org.apache.geronimo.security.jacc.GeronimoPolicy");
  372           start(securityServiceGBean);
  373   
  374           builder = new TomcatModuleBuilder(new URI("null"), containerName, null,
  375                   kernel);
  376   
  377           // Default Realm
  378           Map initParams = new HashMap();
  379   
  380           initParams.put("userClassNames",
  381                           "org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal");
  382           initParams.put("roleClassNames",
  383                           "org.apache.geronimo.security.realm.providers.GeronimoGroupPrincipal");
  384           realm = new GBeanData(realmName, RealmGBean.GBEAN_INFO);
  385           realm.setAttribute("className",
  386                   "org.apache.geronimo.tomcat.realm.TomcatJAASRealm");
  387           realm.setAttribute("initParams", initParams);
  388           start(realm);
  389   
  390           // Default Engine
  391           initParams.clear();
  392           initParams.put("name", "Geronimo");
  393           initParams.put("defaultHost", "localhost");
  394           engine = new GBeanData(engineName, EngineGBean.GBEAN_INFO);
  395           engine.setAttribute("className", "org.apache.geronimo.tomcat.TomcatEngine");
  396           engine.setAttribute("initParams", initParams);
  397           engine.setReferencePattern("realmGBean", realmName);
  398           start(engine);
  399   
  400           // Default Host
  401           initParams.clear();
  402           initParams.put("workDir", "work");
  403           initParams.put("name", "localhost");
  404           initParams.put("appBase", "");
  405           host = new GBeanData(hostName, HostGBean.GBEAN_INFO);
  406           host.setAttribute("className", "org.apache.catalina.core.StandardHost");
  407           host.setAttribute("initParams", initParams);
  408           host.setReferencePattern("engineGBean", engineName);
  409           start(host);
  410   
  411           container = new GBeanData(containerName, TomcatContainer.GBEAN_INFO);
  412           container.setAttribute("classLoader", cl);
  413           container.setAttribute("catalinaHome", "target/var/catalina");
  414           container.setReferencePattern("engineGBean", engineName);
  415           container.setReferencePattern("ServerInfo", serverInfoName);
  416   
  417           initParams.clear();
  418           initParams.put("port", "8080");
  419           connector = new GBeanData(connectorName, ConnectorGBean.GBEAN_INFO);
  420           connector.setAttribute("initParams", initParams);
  421           connector.setReferencePattern("TomcatContainer", containerName);
  422   
  423           start(container);
  424           start(connector);
  425   
  426           tm = new GBeanData(tmName, TransactionManagerImpl.GBEAN_INFO);
  427           Set patterns = new HashSet();
  428           patterns.add(ObjectName.getInstance("geronimo.server:j2eeType=JCAManagedConnectionFactory,*"));
  429           tm.setAttribute("defaultTransactionTimeoutSeconds", new Integer(10));
  430           tm.setReferencePatterns("ResourceManagers", patterns);
  431           start(tm);
  432           tcm = new GBeanData(tcmName, TransactionContextManager.GBEAN_INFO);
  433           tcm.setReferencePattern("TransactionManager", tmName);
  434           start(tcm);
  435           ctc = new GBeanData(ctcName, ConnectionTrackingCoordinator.GBEAN_INFO);
  436           start(ctc);
  437   
  438       }
  439   
  440       protected void tearDown() throws Exception {
  441           stop(ctcName);
  442           stop(tmName);
  443           stop(serverInfoName);
  444           stop(securityServiceName);
  445           stop(connectorName);
  446           stop(containerName);
  447           kernel.shutdown();
  448       }
  449   
  450       private void start(GBeanData gbeanData) throws Exception {
  451           kernel.loadGBean(gbeanData, cl);
  452           kernel.startGBean(gbeanData.getName());
  453           if (kernel.getGBeanState(gbeanData.getName()) != State.RUNNING_INDEX) {
  454               fail("gbean not started: " + gbeanData.getName());
  455           }
  456       }
  457   
  458       private void stop(ObjectName name) throws Exception {
  459           kernel.stopGBean(name);
  460           kernel.unloadGBean(name);
  461       }
  462   
  463       public static class MockConfigStore implements ConfigurationStore {
  464           private final Kernel kernel;
  465   
  466           public MockConfigStore(Kernel kernel) {
  467               this.kernel = kernel;
  468           }
  469   
  470           public URI install(URL source) throws IOException, InvalidConfigException {
  471               return null;
  472           }
  473   
  474           public void install(ConfigurationData configurationData, File source) throws IOException, InvalidConfigException {
  475           }
  476   
  477           public void uninstall(URI configID) throws NoSuchConfigException, IOException {
  478           }
  479   
  480           public ObjectName loadConfiguration(URI configId) throws NoSuchConfigException, IOException, InvalidConfigException {
  481               ObjectName configurationObjectName = null;
  482               try {
  483                   configurationObjectName = Configuration.getConfigurationObjectName(configId);
  484               } catch (MalformedObjectNameException e) {
  485                   throw new InvalidConfigException(e);
  486               }
  487               GBeanData configData = new GBeanData(configurationObjectName, Configuration.GBEAN_INFO);
  488               configData.setAttribute("id", configId);
  489               configData.setAttribute("domain", "test");
  490               configData.setAttribute("server", "bar");
  491               configData.setAttribute("gBeanState", NO_OBJECTS_OS);
  492   
  493               try {
  494                   kernel.loadGBean(configData, Configuration.class.getClassLoader());
  495               } catch (Exception e) {
  496                   throw new InvalidConfigException("Unable to register configuration", e);
  497               }
  498   
  499               return configurationObjectName;
  500           }
  501   
  502           public boolean containsConfiguration(URI configID) {
  503               return true;
  504           }
  505   
  506           public void updateConfiguration(ConfigurationData configurationData) throws NoSuchConfigException, Exception {
  507   
  508           }
  509   
  510           public String getObjectName() {
  511               return null;
  512           }
  513   
  514           public List listConfiguations() {
  515               return null;
  516           }
  517   
  518           public File createNewConfigurationDir() {
  519               return null;
  520           }
  521   
  522           public final static GBeanInfo GBEAN_INFO;
  523   
  524           private static final byte[] NO_OBJECTS_OS;
  525   
  526           static {
  527               GBeanInfoBuilder infoBuilder = new GBeanInfoBuilder(MockConfigStore.class, NameFactory.CONFIGURATION_STORE);
  528               infoBuilder.addInterface(ConfigurationStore.class);
  529               infoBuilder.addAttribute("kernel", Kernel.class, false);
  530               infoBuilder.setConstructor(new String[] {"kernel"});
  531               GBEAN_INFO = infoBuilder.getBeanInfo();
  532   
  533               ByteArrayOutputStream baos = new ByteArrayOutputStream();
  534               try {
  535                   ObjectOutputStream oos = new ObjectOutputStream(baos);
  536                   oos.flush();
  537                   NO_OBJECTS_OS = baos.toByteArray();
  538               } catch (IOException e) {
  539                   throw new RuntimeException(e);
  540               }
  541           }
  542       }
  543   }

Home » geronimo-2.2-source-release » org.apache.geronimo.tomcat.deployment » [javadoc | source]