Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 1997-2010 Sun Microsystems, Inc. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
  * may not use this file except in compliance with the License. You can obtain
  * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html
  * or jersey/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at jersey/legal/LICENSE.txt.
  * Sun designates this particular file as subject to the "Classpath" exception
  * as provided by Sun in the GPL Version 2 section of the License file that
  * accompanied this code.  If applicable, add the following below the License
  * Header, with the fields enclosed by brackets [] replaced by your own
  * identifying information: "Portions Copyrighted [year]
  * [name of copyright owner]"
  *
  * Contributor(s):
  *
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 package com.sun.jersey.test.framework.spi.container.embedded.glassfish;
 
 import java.io.File;
 import java.net.URI;
A Web-based test container factory for creating test container instances using Embedded GlassFish.

Author(s):
Srinivas.Bhimisetty@Sun.COM
 
 
     public Class<WebAppDescriptorsupports() {
         return WebAppDescriptor.class;
     }
 
     public TestContainer create(URI baseUriAppDescriptor ad) {
         if (!(ad instanceof WebAppDescriptor))
             throw new IllegalArgumentException(
                     "The application descriptor must be an instance of WebAppDescriptor");
 
         return new EmbeddedGlassFishTestContainer(baseUri, (WebAppDescriptor)ad);
     }

    
This class has methods for instantiating , starting and stopping EmbeddedGlassFish.
 
     private static class EmbeddedGlassFishTestContainer implements TestContainer {
         
         private static final Logger LOGGER =
                 Logger.getLogger(EmbeddedGlassFishTestContainer.class.getName());
 
         private ScatteredArchive war;
 
         private Server server;
 
         private EmbeddedInfo embeddedInfo;
         
         private WebAppDescriptor appDescriptor;
 
         final URI baseUri;
 
        final String WEB_XML = "web.xml";
        final String WEB_INF_PATH"WEB-INF";
        final String TARGET_WEBAPP_PATH = "target/webapp";
        final String SRC_WEBAPP_PATH = "src/main/webapp";
        final String TARGET_CLASSES_PATH = "target/classes";

        
Creates an instance of EmbeddedGlassFishTestContainerFactory.EmbeddedGlassFishTestContainer

Parameters:
Base URI of the application
An instance of com.sun.jersey.test.framework.AppDescriptor
        private EmbeddedGlassFishTestContainer(URI baseUriWebAppDescriptor ad) {
            this. = UriBuilder.fromUri(baseUri)
                    .path(ad.getContextPath())
                    .path(ad.getServletPath())
                    .build();
            this. = ad;
            .info("Creating EmbeddedGlassFish test container configured at the base URI " + this.);
            instantiateServer();
            createArchive();
        }
        public Client getClient() {
            return null;
        }
        public URI getBaseUri() {
            return this.;
        }
        public void start() {
            .info("Starting the EmbeddedGlassFish instance...");
            try {
                .start();
                .getDeployer().deploy(null);
            } catch (EmbeddedException ex) {
                throw new TestContainerException(ex);
            }             
        }
        public void stop() {
            .info("Stopping the EmbeddedGlassFish instance...");
            try {
                .getDeployer().undeployAll();
                .stop();
            } catch (EmbeddedException ex) {
                throw new TestContainerException(ex);
            }             
        }

        
Instantiates EmbeddedGlassFish
        private void instantiateServer() {
             = new EmbeddedInfo();
            .setLogging(false);
            .setHttpPort(this..getPort());
            .setServerName("EmbeddedGFServer");
            //get an instance fof the server
             = Server.getServer("EmbeddedGFServer");
            if( == null) {
                try {
                     = new Server();
                } catch (EmbeddedException ex) {
                    throw new TestContainerException(ex);
                }
            }             
        }

        
Checks is web.xml exists or not, if not generates one on the fly.

Returns:
Whether web.xml is generated on the fly.
        private boolean webXmlGeneratedOnTheFly() {
            if( !webXmlExists() ) {
                File webXmlDir = new File( + "/" + );
                webXmlDir.mkdirs();
                File webXml = new File( + "/" +  +
                        "/" + );
                try {
                    OutputStream outputStream = new FileOutputStream(webXml);
                    WebXmlGenerator webXmlGenerator = new WebXmlGenerator();
                    try {
                        webXmlGenerator.marshalData(outputStream);
                    } catch (JAXBException ex) {
                        throw new TestContainerException(ex);
                    }
                    outputStream.close();
                } catch (FileNotFoundException ex) {
                    throw new TestContainerException(ex);
                } catch (IOException ex) {
                    throw new TestContainerException(ex);
                }
                return true;
            }
            return false;
        }
        private boolean webXmlExists() {
            File webXml = new File( + "/" +  + "/"
                    + );
            return webXml.exists();
        }

        
Creates an archive of the application for deployment.
        private void createArchive() {
            // create an archive of the deployment descriptor and test classes
            if ( !webXmlGeneratedOnTheFly() ) {
                try {
                     = new ScatteredArchive(.getRawPath(),
                            new File(),
                            new File( + "/"  +  + "/" + ),
                            Collections.singleton(new File().toURI().toURL()));
                } catch (MalformedURLException ex) {
                    throw new TestContainerException(ex);
                }
            } else {
                try {
                     = new ScatteredArchive(.getRawPath(),
                            new File(),
                            new File( + "/" +  + "/" + ),
                            Collections.singleton(new File().toURI().toURL()));
                } catch (MalformedURLException ex) {
                    throw new TestContainerException(ex);
                }
            }            
        }
    }
New to GrepCode? Check out our FAQ X