org.glassfish.embeddable.archive.ScatteredEnterpriseArchive Maven / Gradle / Ivy
The newest version!
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2010 Oracle and/or its affiliates. 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://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/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 packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [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 org.glassfish.embeddable.archive;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Abstraction for a Scattered Java EE Application.
*
*
* Usage example :
*
*
*
* GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish();
* glassfish.start();
*
* // Create a scattered web application.
* ScatteredArchive webmodule = new ScatteredArchive("testweb", ScatteredArchive.Type.WAR);
* // target/classes directory contains my complied servlets
* webmodule.addClassPath(new File("target", "classes"));
* // resources/sun-web.xml is my WEB-INF/sun-web.xml
* webmodule.addMetadata(new File("resources", "sun-web.xml"));
*
* // Create a scattered enterprise archive.
* ScatteredEnterpriseArchive archive = new ScatteredEnterpriseArchive("testapp");
* // src/application.xml is my META-INF/application.xml
* archive.addMetadata(new File("src", "application.xml"));
* // Add scattered web module to the scattered enterprise archive.
* // src/application.xml references Web module as "scattered.war". Hence specify the name while adding the archive.
* archive.addArchive(webmodule.toURI(), "scattered.war");
* // lib/mylibrary.jar is a library JAR file.
* archive.addArchive(new File("lib", "mylibrary.jar"));
* // target/ejbclasses contain my compiled EJB module.
* // src/application.xml references EJB module as "ejb.jar". Hence specify the name while adding the archive.
* archive.addArchive(new File("target", "ejbclasses"), "ejb.jar");
*
* Deployer deployer = glassfish.getDeployer();
* // Deploy my scattered web application
* deployer.deploy(webmodule.toURI());
*
*
* @author [email protected]
*/
public class ScatteredEnterpriseArchive {
String name;
static final String type = "ear";
Map archives = new HashMap();
Map metadatas = new HashMap();
/**
* Construct a new scattered enterprise archive.
*
* @param name Name of the enterprise archive.
* @throws NullPointerException if name is null.
*/
public ScatteredEnterpriseArchive(String name) {
if (name == null) {
throw new NullPointerException("name must not be null.");
}
this.name = name;
}
/**
* Add a module or a library to this scattered enterprise archive.
*
* The addArchive(archiveURI) method has the same effect as:
*
* addMetadata(archiveURI, null)
*
* Follows the same semantics as {@link #addArchive(URI, String)} method.
*/
public void addArchive(URI archiveURI) throws IOException {
addArchive(archiveURI, null);
}
/**
* Add a module or a library to this scattered enterprise archive.
*
* The specified archiveURI must be one of the following:
*
* ScatteredArchive URI obtained via {@link ScatteredArchive#toURI()}.
* Location of a library JAR file. Must be a File URI.
* Location of a Java EE module. Must be a File URI.
*
* If the specified name is null, then the name is computed as the name of the
* File as located by archiveURI.
*
* @param archiveURI Module or library archive URI.
* @param name name of the module/library as specified in META-INF/application.xml
* @throws NullPointerException if archiveURI is null
* @throws IOException if the archiveURI location is not found.
*/
public void addArchive(URI archiveURI, String name) throws IOException {
addArchive(archiveURI != null ? new File(archiveURI) : null, name);
}
/**
* Add a module or a library to this scattered enterprise archive.
*
* The addArchive(archive) method has the same effect as:
*
* addArchive(archive, null)
*
* Follows the same semantics as {@link #addArchive(File, String)} method.
* archive must be a file location.
*/
// public void addArchive(String archive) {
// addArchive(archive, null);
// }
/**
* Add a module or a library to this scattered enterprise archive.
*
* Follows the same semantics as {@link #addArchive(File, String)} method.
* archive must be a file location.
*/
// public void addArchive(String archive, String name) {
// addArchive(archive != null ? new File(archive) : null, name);
// }
/**
* Add a module or a library to this scattered enterprise archive.
*
* The addArchive(archive) method has the same effect as:
*
* addArchive(archive, null)
*
* Follows the same semantics as {@link #addArchive(File, String)} method.
*/
public void addArchive(File archive) throws IOException {
addArchive(archive, null);
}
/**
* Add a module or a library to this scattered enterprise archive.
*
* The specified archive location should be one of the following:
*
* Location of a library JAR file.
* Location of a Java EE module.
*
* If the specified name is null, then the name is computed as archive.getName()
*
* @param archive Location of module or library archive.
* @param name name of the module/library as specified in META-INF/application.xml
* @throws NullPointerException if archive is null
* @throws IOException if the archive file is not found
*/
public void addArchive(File archive, String name) throws IOException {
if (archive == null) {
throw new NullPointerException("archive must not be null.");
}
if (!archive.exists()) {
throw new FileNotFoundException(archive + " does not exist.");
}
// if (archive.isDirectory()) {
// throw new IllegalArgumentException(archive + " is a directory.");
// }
if (name == null) {
name = archive.getName();
}
this.archives.put(name, archive);
}
/**
* Add a new metadata to this scattered enterprise archive.
*
* The addMetadata(metadata) method has the same effect as:
*
* addMetadata(metadata, null)
*
* Follows the same semantics as {@link #addMetadata(String, String)} method.
*/
// public void addMetadata(String metadata) {
// addMetadata(metadata, null);
// }
/**
* Add a new metadata to this scattered enterprise archive.
*
* The addMetadata(metadata) method has the same effect as:
*
* addMetadata(metadata, null)
*
* Follows the same semantics as {@link #addMetadata(File, String)} method.
*/
public void addMetadata(File metadata) throws IOException {
addMetadata(metadata, null);
}
/**
* Add a new metadata to this enterprise archive.
*
* Follows the same semantics as {@link #addMetadata(File, String)} method.
* metatdata must be a file location.
*/
// public void addMetadata(String metadata, String name) {
// addMetadata(metadata != null ? new File(metadata) : null, name);
// }
/**
* Add a new metadata to this enterprise archive.
*
* A metadata is identified by its name (e.g., META-INF/application.xml)
* If the specified name is null, then the name is computed as
* "META-INF/" + metadata.getName()
*
* If the scattered enterprise archive already contains the metadata with
* the same name, the old value is replaced.
*
* @param metadata location of metdata.
* @param name name of the metadata (e.g., META-INF/application.xml)
* @throws NullPointerException if metadata is null
* @throws IOException if metadata is not found
* @throws IllegalArgumentException if metadata is a directory.
*/
public void addMetadata(File metadata, String name) throws IOException {
if (metadata == null) {
throw new NullPointerException("metadata must not be null.");
}
if (!metadata.exists()) {
throw new IOException(metadata + " does not exist.");
}
if (metadata.isDirectory()) {
throw new IllegalArgumentException(metadata + " is a directory.");
}
if (name == null) {
name = "META-INF/" + metadata.getName();
}
this.metadatas.put(name, metadata);
}
/**
* Get the deployable URI for this scattered enterprise archive.
*
* Note : java.io.tmpdir is used while building the URI.
*
* @return Deployable scattered enterprise Archive URI.
* @throws IOException if any I/O error happens while building the URI
* or while reading metadata, archives.
*/
public URI toURI() throws IOException {
return new Assembler().assemble(this);
}
}