use of org.glassfish.deployment.common.ModuleDescriptor in project Payara by payara.
the class UndeployedLaunchable method newUndeployedLaunchable.
static UndeployedLaunchable newUndeployedLaunchable(final ServiceLocator habitat, final ReadableArchive ra, final String callerSuppliedMainClassName, final String callerSuppliedAppName, final ClassLoader classLoader) throws IOException, SAXParseException, UserError {
ArchivistFactory af = Util.getArchivistFactory();
/*
* Try letting the factory decide what type of archive this is. That
* will often allow an app client or an EAR archive to be detected
* automatically.
*/
Archivist archivist = af.getArchivist(ModuleType.CAR.toString(), classLoader);
if (archivist == null) {
throw new UserError(localStrings.get("appclient.invalidArchive", ra.getURI().toASCIIString()));
}
final ArchiveType moduleType = archivist.getModuleType();
if (moduleType != null && moduleType.equals(DOLUtils.carType())) {
return new UndeployedLaunchable(habitat, ra, (AppClientArchivist) archivist, callerSuppliedMainClassName);
} else if (moduleType != null && moduleType.equals(DOLUtils.earType())) {
/*
* Locate the app client submodule that matches the main class name
* or the app client name.
*/
Application app = (Application) archivist.open(ra);
for (ModuleDescriptor<BundleDescriptor> md : app.getModules()) {
if (!md.getModuleType().equals(DOLUtils.carType())) {
continue;
}
ApplicationClientDescriptor acd = (ApplicationClientDescriptor) md.getDescriptor();
final String displayName = acd.getDisplayName();
final String appName = acd.getModuleID();
ArchiveFactory archiveFactory = Util.getArchiveFactory();
ReadableArchive clientRA = archiveFactory.openArchive(ra.getURI().resolve(md.getArchiveUri()));
/*
* Choose this nested app client if the caller-supplied name
* matches, or if the caller-supplied main class matches, or
* if neither was provided.
*/
final boolean useThisClient = (displayName != null && displayName.equals(callerSuppliedAppName)) || (appName != null && appName.equals(callerSuppliedAppName)) || (callerSuppliedMainClassName != null && clientRA.exists(classToResource(callerSuppliedMainClassName)) || (callerSuppliedAppName == null && callerSuppliedMainClassName == null));
if (useThisClient) {
return new UndeployedLaunchable(habitat, clientRA, acd, callerSuppliedMainClassName);
}
clientRA.close();
}
throw new UserError(localStrings.get("appclient.noMatchingClientInEAR", ra.getURI(), callerSuppliedMainClassName, callerSuppliedAppName));
} else {
/*
* There is a possibility that the user is trying to launch an
* archive that is more than one type of archive: such as an EJB
* but also an app client (because the manifest identifies a main
* class, for example).
*
* Earlier the archivist factory might have returned the other type
* of archivist - such as the EJB archivist. Now see if the app
* client archivist will work when selected directly.
*/
archivist = af.getArchivist(DOLUtils.carType());
/*
* Try to open the archive as an app client archive just to see
* if it works.
*/
RootDeploymentDescriptor tempACD = archivist.open(ra);
if (tempACD != null && tempACD instanceof ApplicationClientDescriptor) {
/*
* Start with a fresh archivist - unopened - so we can request
* anno processing, etc. before opening it for real.
*/
archivist = af.getArchivist(DOLUtils.carType());
return new UndeployedLaunchable(habitat, ra, (AppClientArchivist) archivist, callerSuppliedMainClassName);
}
throw new UserError(localStrings.get("appclient.unexpectedArchive", ra.getURI()));
}
}
use of org.glassfish.deployment.common.ModuleDescriptor in project Payara by payara.
the class DOLUtils method getLibraryJarURIs.
/**
* @param bundleDesc
* @param archive
* @return an empty list if bundleDesc is null
* @throws Exception
*/
public static List<URI> getLibraryJarURIs(BundleDescriptor bundleDesc, ReadableArchive archive) throws Exception {
if (bundleDesc == null) {
return Collections.emptyList();
}
ModuleDescriptor moduleDesc = ((BundleDescriptor) bundleDesc).getModuleDescriptor();
Application app = ((BundleDescriptor) moduleDesc.getDescriptor()).getApplication();
return getLibraryJarURIs(app, archive);
}
use of org.glassfish.deployment.common.ModuleDescriptor in project Payara by payara.
the class ApplicationArchivist method getApplicationFromIntrospection.
/**
* This method introspect an ear file and populate the Application object. We follow the Java EE platform specification,
* Section EE.8.4.2 to determine the type of the modules included in this application.
*
* @param archive
* the archive representing the application root
* @param directory
* whether this is a directory deployment
*/
private Application getApplicationFromIntrospection(ReadableArchive archive, boolean directory) {
// archive is a directory
String appRoot = archive.getURI().getSchemeSpecificPart();
if (appRoot.endsWith(File.separator)) {
appRoot = appRoot.substring(0, appRoot.length() - 1);
}
Application app = Application.createApplication();
app.setLoadedFromApplicationXml(false);
app.setVirtual(false);
// name of the file without its extension
String appName = appRoot.substring(appRoot.lastIndexOf(File.separatorChar) + 1);
app.setName(appName);
List<ReadableArchive> unknowns = new ArrayList<ReadableArchive>();
File[] files = getEligibleEntries(new File(appRoot), directory);
for (File subModule : files) {
ReadableArchive subArchive = null;
try {
try {
subArchive = archiveFactory.openArchive(subModule);
} catch (IOException ex) {
logger.log(Level.WARNING, ex.getMessage());
}
// for archive deployment, we check the sub archives by its
// file extension; for directory deployment, we check the sub
// directories by its name. We are now supporting directory
// names with both "_suffix" and ".suffix".
// Section EE.8.4.2.1.a
String name = subModule.getName();
String uri = deriveArchiveUri(appRoot, subModule, directory);
if ((!directory && name.endsWith(".war")) || (directory && (name.endsWith("_war") || name.endsWith(".war")))) {
ModuleDescriptor<BundleDescriptor> md = new ModuleDescriptor<BundleDescriptor>();
md.setArchiveUri(uri);
md.setModuleType(DOLUtils.warType());
// the context root will be set later after
// we process the sub modules
app.addModule(md);
} else // Section EE.8.4.2.1.b
if ((!directory && name.endsWith(".rar")) || (directory && (name.endsWith("_rar") || name.endsWith(".rar")))) {
ModuleDescriptor<BundleDescriptor> md = new ModuleDescriptor<BundleDescriptor>();
md.setArchiveUri(uri);
md.setModuleType(DOLUtils.rarType());
app.addModule(md);
} else if ((!directory && name.endsWith(".jar")) || (directory && (name.endsWith("_jar") || name.endsWith(".jar")))) {
try {
// Section EE.8.4.2.1.d.i
AppClientArchivist acArchivist = new AppClientArchivist();
if (acArchivist.hasStandardDeploymentDescriptor(subArchive) || acArchivist.hasRuntimeDeploymentDescriptor(subArchive) || acArchivist.getMainClassName(subArchive.getManifest()) != null) {
ModuleDescriptor<BundleDescriptor> md = new ModuleDescriptor<BundleDescriptor>();
md.setArchiveUri(uri);
md.setModuleType(DOLUtils.carType());
md.setManifest(subArchive.getManifest());
app.addModule(md);
continue;
}
// Section EE.8.4.2.1.d.ii
Archivist ejbArchivist = archivistFactory.get().getArchivist(DOLUtils.ejbType());
if (ejbArchivist.hasStandardDeploymentDescriptor(subArchive) || ejbArchivist.hasRuntimeDeploymentDescriptor(subArchive)) {
ModuleDescriptor<BundleDescriptor> md = new ModuleDescriptor<BundleDescriptor>();
md.setArchiveUri(uri);
md.setModuleType(DOLUtils.ejbType());
app.addModule(md);
continue;
}
} catch (IOException ex) {
logger.log(Level.WARNING, ex.getMessage());
}
// Still could not decide between an ejb and a library
unknowns.add(subArchive);
// Prevent this unknown archive from being closed in the
// finally block, because the same object will be used in
// the block below where unknowns are checked one more time.
subArchive = null;
} else {
// ignored
}
} finally {
if (subArchive != null) {
try {
subArchive.close();
} catch (IOException ioe) {
logger.log(Level.WARNING, localStrings.getLocalString("enterprise.deployment.errorClosingSubArch", "Error closing subarchive {0}", new Object[] { subModule.getAbsolutePath() }), ioe);
}
}
}
}
if (unknowns.size() > 0) {
AnnotationDetector detector = new AnnotationDetector(new EjbComponentAnnotationScanner());
for (int i = 0; i < unknowns.size(); i++) {
File jarFile = new File(unknowns.get(i).getURI().getSchemeSpecificPart());
try {
if (detector.hasAnnotationInArchive(unknowns.get(i))) {
String uri = deriveArchiveUri(appRoot, jarFile, directory);
// Section EE.8.4.2.1.d.ii, alas EJB
ModuleDescriptor<BundleDescriptor> md = new ModuleDescriptor<BundleDescriptor>();
md.setArchiveUri(uri);
md.setModuleType(DOLUtils.ejbType());
app.addModule(md);
}
/*
* The subarchive was opened by the anno detector. Close it.
*/
unknowns.get(i).close();
} catch (IOException ex) {
logger.log(Level.WARNING, ex.getMessage());
}
}
}
return app;
}
use of org.glassfish.deployment.common.ModuleDescriptor in project Payara by payara.
the class ApplicationArchivist method readRuntimeDeploymentDescriptor.
/**
* Read the runtime deployment descriptors (can contained in one or many file) set the corresponding information in the
* passed descriptor. By default, the runtime deployment descriptors are all contained in the xml file characterized
* with the path returned by
*
* @param archive
* the input archive
* @param descriptor
* the initialized deployment descriptor
*/
@Override
public void readRuntimeDeploymentDescriptor(ReadableArchive archive, Application descriptor) throws IOException, SAXParseException {
if (descriptor != null) {
// each modules first...
for (ModuleDescriptor md : descriptor.getModules()) {
Archivist archivist = archivistFactory.get().getArchivist(md.getModuleType());
archivist.initializeContext(this);
archivist.setRuntimeXMLValidation(this.getRuntimeXMLValidation());
archivist.setRuntimeXMLValidationLevel(this.getRuntimeXMLValidationLevel());
ReadableArchive subArchive = archive.getSubArchive(md.getArchiveUri());
if (md.getAlternateDescriptor() != null) {
DOLUtils.readAlternativeRuntimeDescriptor(archive, subArchive, archivist, (BundleDescriptor) md.getDescriptor(), md.getAlternateDescriptor());
} else {
archivist.readRuntimeDeploymentDescriptor(subArchive, (BundleDescriptor) md.getDescriptor());
}
}
}
// for the application
super.readRuntimeDeploymentDescriptor(archive, descriptor);
}
use of org.glassfish.deployment.common.ModuleDescriptor in project Payara by payara.
the class ApplicationArchivist method writeContents.
/**
* writes the content of an archive to a JarFile
*
* @param in
* the descriptors to use for writing
* @param out
* the output stream to write to
*/
@Override
protected void writeContents(ReadableArchive in, WritableArchive out) throws IOException {
Vector filesToSkip = new Vector();
if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) {
DOLUtils.getDefaultLogger().fine("Write " + out.getURI() + " with " + this);
}
// any files already written to the output should never be rewritten
for (Enumeration alreadyWritten = out.entries(); alreadyWritten.hasMoreElements(); ) {
String elementName = (String) alreadyWritten.nextElement();
filesToSkip.add(elementName);
}
// write this application .ear file contents...
for (ModuleDescriptor aModule : descriptor.getModules()) {
Archivist subArchivist = archivistFactory.get().getArchivist(aModule.getModuleType());
subArchivist.initializeContext(this);
subArchivist.setModuleDescriptor(aModule);
if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) {
DOLUtils.getDefaultLogger().info("Write " + aModule.getArchiveUri() + " with " + subArchivist);
}
// Create a new jar file inside the application .ear
WritableArchive internalJar = out.createSubArchive(aModule.getArchiveUri());
// we need to copy the old archive to a temp file so
// the save method can copy its original contents from
File tmpFile = null;
BufferedOutputStream bos = null;
try (InputStream is = in.getEntry(aModule.getArchiveUri())) {
if (in instanceof WritableArchive) {
subArchivist.setArchiveUri(internalJar.getURI().getSchemeSpecificPart());
} else {
tmpFile = getTempFile(path);
bos = new BufferedOutputStream(new FileOutputStream(tmpFile));
ArchivistUtils.copy(is, bos);
// configure archivist
subArchivist.setArchiveUri(tmpFile.getAbsolutePath());
}
subArchivist.writeContents(internalJar);
out.closeEntry(internalJar);
} finally {
if (tmpFile != null) {
boolean ok = tmpFile.delete();
if (!ok) {
logger.log(Level.WARNING, localStrings.getLocalString("enterprise.deployment.cantDelete", "Error deleting file {0}", new Object[] { tmpFile.getAbsolutePath() }));
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException ioe) {
// ignore
}
}
}
// no need to copy the bundle from the original jar file
filesToSkip.add(aModule.getArchiveUri());
}
// now write the old contents and new descriptors
super.writeContents(in, out, filesToSkip);
}
Aggregations