use of org.apache.openejb.config.sys.Deployments in project tomee by apache.
the class ConfigurationFactory method toConfigDeclaration.
public static Object toConfigDeclaration(final String id, final URI uri) throws OpenEJBException {
final String serviceType;
try {
serviceType = uri.getHost();
final Object object;
try {
object = JaxbOpenejb.create(serviceType);
} catch (final Exception e) {
throw new OpenEJBException("Invalid URI '" + uri + "'. " + e.getMessage());
}
final Map<String, String> map;
try {
map = URISupport.parseParamters(uri);
} catch (final URISyntaxException e) {
throw new OpenEJBException("Unable to parse URI parameters '" + uri + "'. URISyntaxException: " + e.getMessage());
}
if (object instanceof AbstractService) {
final AbstractService service = (AbstractService) object;
service.setId(id);
service.setType(map.remove("type"));
service.setProvider(map.remove("provider"));
service.setClassName(map.remove("class-name"));
service.setConstructor(map.remove("constructor"));
service.setFactoryName(map.remove("factory-name"));
service.setPropertiesProvider(map.remove("properties-provider"));
service.setTemplate(map.remove("template"));
final String cp = map.remove("classpath");
if (null != cp) {
service.setClasspath(cp);
}
service.setClasspathAPI(map.remove("classpath-api"));
if (object instanceof Resource) {
final Resource resource = Resource.class.cast(object);
final String aliases = map.remove("aliases");
if (aliases != null) {
resource.getAliases().addAll(Arrays.asList(aliases.split(",")));
}
final String depOn = map.remove("depends-on");
if (depOn != null) {
resource.getDependsOn().addAll(Arrays.asList(depOn.split(",")));
}
resource.setPostConstruct(map.remove("post-construct"));
resource.setPreDestroy(map.remove("pre-destroy"));
}
service.getProperties().putAll(map);
} else if (object instanceof Deployments) {
final Deployments deployments = (Deployments) object;
deployments.setDir(map.remove("dir"));
deployments.setFile(map.remove("jar"));
final String cp = map.remove("classpath");
if (cp != null) {
final String[] paths = cp.split(File.pathSeparator);
final List<URL> urls = new ArrayList<>();
for (final String path : paths) {
final Set<String> values = ProvisioningUtil.realLocation(PropertyPlaceHolderHelper.value(path));
for (final String v : values) {
urls.add(new File(v).toURI().normalize().toURL());
}
}
deployments.setClasspath(new URLClassLoaderFirst(urls.toArray(new URL[urls.size()]), ParentClassLoaderFinder.Helper.get()));
}
} else if (SystemProperty.class.isInstance(object)) {
final SystemProperty sp = SystemProperty.class.cast(object);
sp.setName(map.remove("name"));
sp.setValue(map.remove("value"));
}
return object;
} catch (final Exception e) {
throw new OpenEJBException("Error declaring service '" + id + "'. Unable to create Service definition from URI '" + uri.toString() + "'", e);
}
}
use of org.apache.openejb.config.sys.Deployments in project tomee by apache.
the class ConfigUtils method addDeploymentEntryToConfig.
public static boolean addDeploymentEntryToConfig(final String jarLocation, final Openejb config) {
final File jar = new File(jarLocation);
/* Check to see if the entry is already listed */
for (final Deployments d : config.getDeployments()) {
if (d.getFile() != null) {
try {
final File target = SystemInstance.get().getBase().getFile(d.getFile(), false);
/*
* If the jar entry is already there, no need
* to add it to the config or go any futher.
*/
if (jar.equals(target)) {
return false;
}
} catch (final IOException e) {
/* No handling needed. If there is a problem
* resolving a config file path, it is better to
* just add this jars path explicitly.
*/
}
} else if (d.getDir() != null) {
try {
final File target = SystemInstance.get().getBase().getFile(d.getDir(), false);
final File jarDir = jar.getAbsoluteFile().getParentFile();
/*
* If a dir entry is already there, the jar
* will be loaded automatically. No need
* to add it explicitly to the config or go
* any futher.
*/
if (jarDir != null && jarDir.equals(target)) {
return false;
}
} catch (final IOException e) {
/* No handling needed. If there is a problem
* resolving a config file path, it is better to
* just add this jars path explicitly.
*/
}
}
}
/* Create a new Deployments entry */
final Deployments dep = JaxbOpenejb.createDeployments();
dep.setFile(jarLocation);
config.getDeployments().add(dep);
return true;
}
use of org.apache.openejb.config.sys.Deployments in project tomee by apache.
the class ConfigurationFactoryTest method testToConfigDeclaration.
@Test
public void testToConfigDeclaration() throws Exception {
final String path = ".";
final ConfigurationFactory factory = new ConfigurationFactory();
final Deployments deployments = (Deployments) factory.toConfigDeclaration("", new URI("new://Deployments?classpath=" + path));
final URLClassLoader cl = (URLClassLoader) deployments.getClasspath();
final URL[] urls = cl.getURLs();
assertEquals(urls[0], new File(path).toURI().normalize().toURL());
}
use of org.apache.openejb.config.sys.Deployments in project tomee by apache.
the class AbstractTomEEMojo method ensureAppsFolderExistAndIsConfiguredByDefault.
private void ensureAppsFolderExistAndIsConfiguredByDefault(final File file) throws IOException {
if ("openejb".equals(container.toLowerCase(Locale.ENGLISH)) || (file.exists() && ((apps != null && !apps.isEmpty()) || (!"pom".equals(packaging) && !"war".equals(packaging))))) {
// webapps doesn't need apps folder in tomee
final String rootTag = container.toLowerCase(Locale.ENGLISH);
if (file.isFile()) {
// can be not existing since we dont always deploy tomee but shouldn't since then apps/ is not guaranteed to work
try {
final Openejb jaxb = JaxbOpenejb.readConfig(file.getAbsolutePath());
boolean needAdd = true;
for (final Deployments d : jaxb.getDeployments()) {
if ("apps".equals(d.getDir())) {
needAdd = false;
break;
}
}
if (needAdd) {
final String content = IO.slurp(file);
final FileWriter writer = new FileWriter(file);
final String end = "</" + rootTag + ">";
writer.write(content.replace(end, " <Deployments dir=\"apps\" />\n" + end));
writer.close();
}
} catch (final OpenEJBException e) {
throw new IllegalStateException("illegal tomee.xml:\n" + IO.slurp(file), e);
}
} else {
final FileWriter writer = new FileWriter(file);
writer.write("<?xml version=\"1.0\"?>\n" + "<" + rootTag + ">\n" + " <Deployments dir=\"apps\" />\n" + "</" + rootTag + ">\n");
writer.close();
}
final File appsFolder = new File(catalinaBase, "apps");
if (!appsFolder.exists() && !appsFolder.mkdirs()) {
throw new RuntimeException("Failed to create: " + appsFolder);
}
}
}
use of org.apache.openejb.config.sys.Deployments in project tomee by apache.
the class ConfigurationFactory method getDeclaredApps.
private List<File> getDeclaredApps() {
// make a copy of the list because we update it
final List<Deployments> deployments = new ArrayList<Deployments>();
if (openejb != null) {
deployments.addAll(openejb.getDeployments());
}
Collection<Deployments> additionalDeploymentsList = Collections.emptyList();
try {
final File additionalDeploymentFile = SystemInstance.get().getBase().getFile(ADDITIONAL_DEPLOYMENTS, false);
if (additionalDeploymentFile.exists()) {
InputStream fis = null;
try {
fis = IO.read(additionalDeploymentFile);
final AdditionalDeployments additionalDeployments = JaxbOpenejb.unmarshal(AdditionalDeployments.class, fis);
additionalDeploymentsList = additionalDeployments.getDeployments();
} catch (final Exception e) {
logger.error("can't read " + ADDITIONAL_DEPLOYMENTS, e);
} finally {
IO.close(fis);
}
}
} catch (final Exception e) {
logger.info("No additional deployments found: " + e);
}
// resolve jar locations ////////////////////////////////////// BEGIN ///////
final FileUtils base = SystemInstance.get().getBase();
final List<Deployments> autoDeploy = new ArrayList<Deployments>();
final List<File> declaredAppsUrls = new ArrayList<File>();
for (final Deployments deployment : deployments) {
try {
DeploymentsResolver.loadFrom(deployment, base, declaredAppsUrls);
if (deployment.isAutoDeploy()) {
autoDeploy.add(deployment);
}
} catch (final SecurityException se) {
logger.warning("Security check failed on deployment: " + deployment.getFile(), se);
}
}
for (final Deployments additionalDep : additionalDeploymentsList) {
if (additionalDep.getFile() != null) {
declaredAppsUrls.add(Files.path(base.getDirectory().getAbsoluteFile(), additionalDep.getFile()));
} else if (additionalDep.getDir() != null) {
declaredAppsUrls.add(Files.path(base.getDirectory().getAbsoluteFile(), additionalDep.getDir()));
}
if (additionalDep.isAutoDeploy()) {
autoDeploy.add(additionalDep);
}
}
if (autoDeploy.size() > 0) {
final AutoDeployer autoDeployer = new AutoDeployer(this, autoDeploy);
SystemInstance.get().setComponent(AutoDeployer.class, autoDeployer);
SystemInstance.get().addObserver(autoDeployer);
}
return declaredAppsUrls;
}
Aggregations