use of org.apache.openejb.config.sys.AdditionalDeployments in project tomee by apache.
the class DeployerEjb method saveDeployment.
private synchronized void saveDeployment(final File file, final boolean add) {
final Deployments deps = new Deployments();
if (file.isDirectory()) {
deps.setDir(file.getAbsolutePath());
} else {
deps.setFile(file.getAbsolutePath());
}
File config;
try {
config = SystemInstance.get().getBase().getFile(ADDITIONAL_DEPLOYMENTS, false);
} catch (final IOException e) {
config = null;
}
if (config == null || !config.getParentFile().exists()) {
LOGGER.info("Cannot save the added app because the conf folder does not exist, it will not be present on a restart");
return;
}
// dump it
OutputStream os = null;
try {
final AdditionalDeployments additionalDeployments;
if (config.exists() && config.length() > 0) {
final InputStream fis = IO.read(config);
try {
additionalDeployments = JaxbOpenejb.unmarshal(AdditionalDeployments.class, fis);
} finally {
IO.close(fis);
}
} else {
additionalDeployments = new AdditionalDeployments();
}
if (add) {
if (!additionalDeployments.getDeployments().contains(deps)) {
additionalDeployments.getDeployments().add(deps);
}
} else {
final Iterator<Deployments> it = additionalDeployments.getDeployments().iterator();
while (it.hasNext()) {
final Deployments current = it.next();
if (deps.getDir() != null && deps.getDir().equals(current.getDir())) {
it.remove();
break;
} else if (deps.getFile() != null && deps.getFile().equals(current.getFile())) {
it.remove();
break;
} else {
// exploded dirs
final String jar = deps.getFile();
if (jar != null && jar.length() > 3) {
final String substring = jar.substring(0, jar.length() - 4);
if (substring.equals(current.getDir()) || substring.equals(current.getFile())) {
it.remove();
break;
}
} else {
final String jarC = current.getFile();
if (jarC != null && jarC.length() > 3) {
final String substring = jarC.substring(0, jarC.length() - 4);
if (substring.equals(deps.getDir()) || substring.equals(deps.getFile())) {
it.remove();
break;
}
}
}
}
}
}
os = IO.write(config);
JaxbOpenejb.marshal(AdditionalDeployments.class, additionalDeployments, os);
} catch (final Exception e) {
LOGGER.error("cannot save the added app, will not be present next time you'll start", e);
} finally {
IO.close(os);
}
}
use of org.apache.openejb.config.sys.AdditionalDeployments in project tomee by apache.
the class DeployerEjbTest method removeDeploymentsLogic.
@Test
public void removeDeploymentsLogic() throws Exception {
final File dir1 = Files.mkdirs(new File("target/DeployerEjbTest/removeDeploymentsLogic/app1/"));
final File file = SystemInstance.get().getBase().getFile(ADDITIONAL_DEPLOYMENTS, false);
final Method save = DeployerEjb.class.getDeclaredMethod("saveDeployment", File.class, boolean.class);
save.setAccessible(true);
{
final AdditionalDeployments deployments = new AdditionalDeployments();
final Deployments d1 = new Deployments();
d1.setDir(dir1.getCanonicalPath());
deployments.getDeployments().add(d1);
final Deployments d12 = new Deployments();
d12.setDir(dir1.getCanonicalPath());
deployments.getDeployments().add(d12);
final Deployments d2 = new Deployments();
d2.setFile(new File(File.listRoots()[0], "/foo/bar/app.war").getAbsolutePath());
deployments.getDeployments().add(d2);
try (final FileOutputStream fos = new FileOutputStream(file)) {
JaxbOpenejb.marshal(AdditionalDeployments.class, deployments, fos);
}
assertDeployementsSize(file, 3);
}
{
save.invoke(new DeployerEjb(), dir1, false);
assertDeployementsSize(file, 2);
}
{
save.invoke(new DeployerEjb(), new File(dir1.getParentFile(), dir1.getName() + ".war"), false);
assertDeployementsSize(file, 1);
}
{
save.invoke(new DeployerEjb(), new File(File.listRoots()[0], "/foo/bar/app.war"), false);
assertDeployementsSize(file, 0);
}
}
use of org.apache.openejb.config.sys.AdditionalDeployments 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<>();
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<>();
final List<File> declaredAppsUrls = new ArrayList<>();
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