use of org.apache.aries.util.manifest.BundleManifest in project aries by apache.
the class AriesApplicationManagerImplTest method createApplication.
private AriesApplication createApplication(String fileName) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, ManagementException, ResolverException {
// This next block is a very long winded way of constructing a BundleInfoImpl
// against the existing (BundleManifest bm, String location) constructor. If we
// find we need a String-based BundleInfoImpl constructor for other reasons,
// we could change to using it here.
Set<BundleInfo> nextResolverResult = new HashSet<BundleInfo>();
String persistenceLibraryLocation = "../src/test/resources/bundles/repository/a.handy.persistence.library.jar";
File persistenceLibrary = new File(persistenceLibraryLocation);
BundleManifest mf = BundleManifest.fromBundle(persistenceLibrary);
BundleInfo resolvedPersistenceLibrary = new SimpleBundleInfo(mf, persistenceLibraryLocation);
Field v = SimpleBundleInfo.class.getDeclaredField("_version");
v.setAccessible(true);
v.set(resolvedPersistenceLibrary, new Version("1.1.0"));
nextResolverResult.add(resolvedPersistenceLibrary);
_resolver.setNextResult(nextResolverResult);
IDirectory testEba = FileSystem.getFSRoot(new File(fileName));
AriesApplication app = _appMgr.createApplication(testEba);
app = _appMgr.resolve(app);
return app;
}
use of org.apache.aries.util.manifest.BundleManifest in project aries by apache.
the class AriesApplicationManagerImpl method createApplication.
/**
* Create an AriesApplication from a .eba file: a zip file with a '.eba' extension
*/
public AriesApplication createApplication(IDirectory ebaFile) throws ManagementException {
ApplicationMetadata applicationMetadata = null;
DeploymentMetadata deploymentMetadata = null;
Map<String, BundleConversion> modifiedBundles = new HashMap<String, BundleConversion>();
AriesApplicationImpl application = null;
String appPath = ebaFile.toString();
try {
// try to read the app name out of the application.mf
Manifest applicationManifest = parseApplicationManifest(ebaFile);
String appName = applicationManifest.getMainAttributes().getValue(AppConstants.APPLICATION_NAME);
// If the application name is null, we will try to get the file name.
if (appName == null || appName.isEmpty()) {
String fullPath = appPath;
if (fullPath.endsWith("/")) {
fullPath = fullPath.substring(0, fullPath.length() - 1);
}
int last_slash = fullPath.lastIndexOf("/");
appName = fullPath.substring(last_slash + 1, fullPath.length());
}
IFile deploymentManifest = ebaFile.getFile(AppConstants.DEPLOYMENT_MF);
/* We require that all other .jar and .war files included by-value be valid bundles
* because a DEPLOYMENT.MF has been provided. If no DEPLOYMENT.MF, migrate
* wars to wabs, plain jars to bundles
*/
Set<BundleInfo> extraBundlesInfo = new HashSet<BundleInfo>();
for (IFile f : ebaFile) {
if (f.isDirectory()) {
continue;
}
BundleManifest bm = getBundleManifest(f);
if (bm != null) {
if (bm.isValid()) {
_logger.debug("File {} is a valid bundle. Adding it to bundle list.", f.getName());
extraBundlesInfo.add(new SimpleBundleInfo(bm, f.toURL().toExternalForm()));
} else if (deploymentManifest == null) {
_logger.debug("File {} is not a valid bundle. Attempting to convert it.", f.getName());
// We have a jar that needs converting to a bundle, or a war to migrate to a WAB
// We only do this if a DEPLOYMENT.MF does not exist.
BundleConversion convertedBinary = null;
Iterator<BundleConverter> converters = _bundleConverters.iterator();
List<ConversionException> conversionExceptions = Collections.emptyList();
while (converters.hasNext() && convertedBinary == null) {
try {
BundleConverter converter = converters.next();
_logger.debug("Converting file using {} converter", converter);
convertedBinary = converter.convert(ebaFile, f);
} catch (ServiceException sx) {
// We'll get this if our optional BundleConverter has not been injected.
} catch (ConversionException cx) {
conversionExceptions.add(cx);
}
}
if (conversionExceptions.size() > 0) {
for (ConversionException cx : conversionExceptions) {
_logger.error("APPMANAGEMENT0004E", new Object[] { f.getName(), appName, cx });
}
throw new ManagementException(MessageUtil.getMessage("APPMANAGEMENT0005E", appName));
}
if (convertedBinary != null) {
_logger.debug("File {} was successfully converted. Adding it to bundle list.", f.getName());
modifiedBundles.put(f.getName(), convertedBinary);
extraBundlesInfo.add(convertedBinary.getBundleInfo());
} else {
_logger.debug("File {} was not converted.", f.getName());
}
} else {
_logger.debug("File {} was ignored. It is not a valid bundle and DEPLOYMENT.MF is present", f.getName());
}
} else {
_logger.debug("File {} was ignored. It has no manifest file.", f.getName());
}
}
// if Application-Content header was not specified build it based on the bundles included by value
if (applicationManifest.getMainAttributes().getValue(AppConstants.APPLICATION_CONTENT) == null) {
String appContent = buildAppContent(extraBundlesInfo);
applicationManifest.getMainAttributes().putValue(AppConstants.APPLICATION_CONTENT, appContent);
}
ManifestDefaultsInjector.updateManifest(applicationManifest, appName, ebaFile);
applicationMetadata = _applicationMetadataFactory.createApplicationMetadata(applicationManifest);
if (deploymentManifest != null) {
deploymentMetadata = _deploymentMetadataFactory.parseDeploymentMetadata(deploymentManifest);
// Validate: symbolic names must match
String appSymbolicName = applicationMetadata.getApplicationSymbolicName();
String depSymbolicName = deploymentMetadata.getApplicationSymbolicName();
if (!appSymbolicName.equals(depSymbolicName)) {
throw new ManagementException(MessageUtil.getMessage("APPMANAGEMENT0002E", appName, appSymbolicName, depSymbolicName));
}
}
application = new AriesApplicationImpl(applicationMetadata, extraBundlesInfo, _localPlatform);
application.setDeploymentMetadata(deploymentMetadata);
// Store a reference to any modified bundles
application.setModifiedBundles(modifiedBundles);
} catch (IOException iox) {
_logger.error("APPMANAGEMENT0006E", new Object[] { appPath, iox });
throw new ManagementException(iox);
}
return application;
}
use of org.apache.aries.util.manifest.BundleManifest in project aries by apache.
the class AriesApplicationManagerImpl method getBundleManifest.
/**
* Extract a bundle manifest from an IFile representing a bundle
* @param file The bundle to extract the manifest from
* @return bundle manifest
*/
private BundleManifest getBundleManifest(IFile file) throws IOException {
BundleManifest mf = null;
InputStream in = null;
try {
in = file.open();
mf = BundleManifest.fromBundle(in);
} finally {
IOUtils.close(in);
}
return mf;
}
use of org.apache.aries.util.manifest.BundleManifest in project aries by apache.
the class EJBModellerTest method testModelServicesExportEJB.
@Test
public void testModelServicesExportEJB() throws ModellerException {
Manifest man = new Manifest();
setBasicHeaders(man);
man.getMainAttributes().putValue("Export-EJB", "anEJB , another");
modeller.modelServices(new BundleManifest(man), bundleLocation);
ejbLocator.assertCalled(new MethodCall(EJBLocator.class, "findEJBs", BundleManifest.class, bundleLocation, ParsedEJBServices.class));
}
use of org.apache.aries.util.manifest.BundleManifest in project aries by apache.
the class ManifestDefaultsInjector method processPossibleBundle.
/**
* This method works out if the given IFile represents an OSGi bundle and if
* it is then we append a matching rule to the String builder.
*
* @param file to file to check.
* @param builder the builder to append to.
*/
private static void processPossibleBundle(IFile file, StringBuilder builder) {
if (file.isDirectory() || (file.isFile() && (file.getName().endsWith(".jar") || file.getName().endsWith(".war")))) {
BundleManifest bundleMf = BundleManifest.fromBundle(file);
if (bundleMf != null) {
String manifestVersion = bundleMf.getManifestVersion();
String name = bundleMf.getSymbolicName();
String version = bundleMf.getVersion().toString();
// if the bundle manifest version is 2 AND a symbolic name is specified we have a valid bundle
if ("2".equals(manifestVersion) && name != null) {
builder.append(name);
// bundle version is not a required manifest header
if (version != null) {
builder.append(";version=\"[");
builder.append(version);
builder.append(',');
builder.append(version);
builder.append("]\"");
}
// the last comma will be removed once all content has been added
builder.append(",");
}
}
}
}
Aggregations