You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by an...@apache.org on 2015/11/07 02:27:23 UTC
[1/3] tomee git commit: Delegate RemoteServer.destroy() to usage -
Try/catch
Repository: tomee
Updated Branches:
refs/heads/tomee-1.7.x e113c06ed -> 6df9839b2
http://git-wip-us.apache.org/repos/asf/tomee/blob/6df9839b/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/FullRestartContainer.java
----------------------------------------------------------------------
diff --git a/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/FullRestartContainer.java b/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/FullRestartContainer.java
index 090a816..f4d5cd1 100644
--- a/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/FullRestartContainer.java
+++ b/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/FullRestartContainer.java
@@ -1,136 +1,137 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.openejb.tck.impl;
-
-import org.apache.openejb.client.RemoteInitialContextFactory;
-import org.apache.openejb.config.RemoteServer;
-import org.apache.openejb.loader.Options;
-import org.apache.openejb.tck.OpenEJBTCKRuntimeException;
-import org.apache.openejb.tck.util.ServerLocal;
-import org.apache.tomee.catalina.facade.ExceptionManagerFacade;
-import org.jboss.testharness.api.DeploymentException;
-import org.jboss.testharness.spi.Containers;
-
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Properties;
-
-/**
- * flow:
- * - copy file
- * - start the server
- * - stop the server
- * - remove the file
- */
-public class FullRestartContainer extends AbstractContainers implements Containers {
- private static final File WEBAPP_DIR = new File(System.getProperty("openejb.home"), "webapps/");
- private static final File APPS_DIR = new File(System.getProperty("openejb.home"), "apps/");
-
- private RemoteServer server;
- private Exception exception;
- private File currentFile;
-
- public FullRestartContainer() {
- System.out.println("ContainersImpl=" + FullRestartContainer.class.getName());
- }
-
- @Override
- public DeploymentException getDeploymentException() {
- if (exception instanceof DeploymentException) {
- return (DeploymentException) exception;
- }
- System.out.println("BADCAST");
- return new DeploymentException("", exception);
- }
-
- @Override
- public boolean deploy(final InputStream archive, final String name) throws IOException {
- if (name.endsWith("war")) {
- currentFile = new File(WEBAPP_DIR, name);
- } else {
- currentFile = new File(APPS_DIR, name);
- }
-
- System.out.println(currentFile);
- writeToFile(currentFile, archive);
-
- final int port = ServerLocal.getPort(-1);
- if (port > 0) {
- server = new RemoteServer(100, true);
- server.setPortStartup(port);
- } else {
- throw new OpenEJBTCKRuntimeException("Please set the tomee port using the system property 'server.http.port'");
- }
-
- try {
- server.start();
- } catch (final RuntimeException e) {
- e.printStackTrace();
- throw e;
- }
-
- return (exception = lookup().exception()) == null;
- }
-
- @Override
- public void undeploy(final String name) throws IOException {
-
- if (null != server) {
- server.destroy();
- }
-
- final File folder = new File(currentFile.getParentFile(), currentFile.getName().substring(0, currentFile.getName().length() - 4));
- if (folder.exists()) {
- delete(folder);
- }
- delete(currentFile);
- }
-
- @Override
- public void setup() throws IOException {
- // no-op
- }
-
- @Override
- public void cleanup() throws IOException {
- // no-op
- }
-
- private ExceptionManagerFacade lookup() {
- final Options options = new Options(System.getProperties());
- final Properties props = new Properties();
- props.put(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
- final int port = ServerLocal.getPort(-1);
- if (port > 0) {
- System.out.println("provider url = " + "http://localhost:" + port + "/tomee/ejb");
- props.put(Context.PROVIDER_URL, options.get(Context.PROVIDER_URL, "http://localhost:" + port + "/tomee/ejb"));
- } else {
- throw new OpenEJBTCKRuntimeException("Please set the tomee port using the system property 'server.http.port'");
- }
-
- try {
- final InitialContext context = new InitialContext(props);
- return (ExceptionManagerFacade) context.lookup("openejb/ExceptionManagerFacadeBusinessRemote");
- } catch (final Exception e) {
- throw new OpenEJBTCKRuntimeException(e);
- }
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.openejb.tck.impl;
+
+import org.apache.openejb.client.RemoteInitialContextFactory;
+import org.apache.openejb.config.RemoteServer;
+import org.apache.openejb.loader.Options;
+import org.apache.openejb.tck.OpenEJBTCKRuntimeException;
+import org.apache.openejb.tck.util.ServerLocal;
+import org.apache.tomee.catalina.facade.ExceptionManagerFacade;
+import org.jboss.testharness.api.DeploymentException;
+import org.jboss.testharness.spi.Containers;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+/**
+ * flow:
+ * - copy file
+ * - start the server
+ * - stop the server
+ * - remove the file
+ */
+public class FullRestartContainer extends AbstractContainers implements Containers {
+ private static final File WEBAPP_DIR = new File(System.getProperty("openejb.home"), "webapps/");
+ private static final File APPS_DIR = new File(System.getProperty("openejb.home"), "apps/");
+
+ private RemoteServer server;
+ private Exception exception;
+ private File currentFile;
+
+ public FullRestartContainer() {
+ System.out.println("ContainersImpl=" + FullRestartContainer.class.getName());
+ }
+
+ @Override
+ public DeploymentException getDeploymentException() {
+ if (exception instanceof DeploymentException) {
+ return (DeploymentException) exception;
+ }
+ System.out.println("BADCAST");
+ return new DeploymentException("", exception);
+ }
+
+ @Override
+ public boolean deploy(final InputStream archive, final String name) throws IOException {
+ if (name.endsWith("war")) {
+ currentFile = new File(WEBAPP_DIR, name);
+ } else {
+ currentFile = new File(APPS_DIR, name);
+ }
+
+ System.out.println(currentFile);
+ writeToFile(currentFile, archive);
+
+ final int port = ServerLocal.getPort(-1);
+ if (port > 0) {
+ server = new RemoteServer(100, true);
+ server.setPortStartup(port);
+ } else {
+ throw new OpenEJBTCKRuntimeException("Please set the tomee port using the system property 'server.http.port'");
+ }
+
+ try {
+ server.start();
+ } catch (final Exception e) {
+ server.destroy();
+ e.printStackTrace();
+ throw new RuntimeException(e);
+ }
+
+ return (exception = lookup().exception()) == null;
+ }
+
+ @Override
+ public void undeploy(final String name) throws IOException {
+
+ if (null != server) {
+ server.destroy();
+ }
+
+ final File folder = new File(currentFile.getParentFile(), currentFile.getName().substring(0, currentFile.getName().length() - 4));
+ if (folder.exists()) {
+ delete(folder);
+ }
+ delete(currentFile);
+ }
+
+ @Override
+ public void setup() throws IOException {
+ // no-op
+ }
+
+ @Override
+ public void cleanup() throws IOException {
+ // no-op
+ }
+
+ private ExceptionManagerFacade lookup() {
+ final Options options = new Options(System.getProperties());
+ final Properties props = new Properties();
+ props.put(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
+ final int port = ServerLocal.getPort(-1);
+ if (port > 0) {
+ System.out.println("provider url = " + "http://localhost:" + port + "/tomee/ejb");
+ props.put(Context.PROVIDER_URL, options.get(Context.PROVIDER_URL, "http://localhost:" + port + "/tomee/ejb"));
+ } else {
+ throw new OpenEJBTCKRuntimeException("Please set the tomee port using the system property 'server.http.port'");
+ }
+
+ try {
+ final InitialContext context = new InitialContext(props);
+ return (ExceptionManagerFacade) context.lookup("openejb/ExceptionManagerFacadeBusinessRemote");
+ } catch (final Exception e) {
+ throw new OpenEJBTCKRuntimeException(e);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/tomee/blob/6df9839b/tomee/apache-tomee/src/main/java/org/apache/tomee/RemoteTomEEEJBContainer.java
----------------------------------------------------------------------
diff --git a/tomee/apache-tomee/src/main/java/org/apache/tomee/RemoteTomEEEJBContainer.java b/tomee/apache-tomee/src/main/java/org/apache/tomee/RemoteTomEEEJBContainer.java
index 1d3f137..1d30c46 100644
--- a/tomee/apache-tomee/src/main/java/org/apache/tomee/RemoteTomEEEJBContainer.java
+++ b/tomee/apache-tomee/src/main/java/org/apache/tomee/RemoteTomEEEJBContainer.java
@@ -1,154 +1,161 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.tomee;
-
-import org.apache.geronimo.osgi.locator.ProviderLocator;
-import org.apache.openejb.OpenEJBException;
-import org.apache.openejb.assembler.Deployer;
-import org.apache.openejb.assembler.DeployerEjb;
-import org.apache.openejb.client.RemoteInitialContextFactory;
-import org.apache.openejb.config.RemoteServer;
-import org.apache.openejb.loader.IO;
-import org.apache.tomee.util.QuickServerXmlParser;
-
-import javax.ejb.EJBException;
-import javax.ejb.embeddable.EJBContainer;
-import javax.ejb.spi.EJBContainerProvider;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.validation.ValidationException;
-import java.io.File;
-import java.io.IOException;
-import java.net.MalformedURLException;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-
-public class RemoteTomEEEJBContainer extends EJBContainer {
- private static RemoteTomEEEJBContainer instance;
- private RemoteServer container;
- private InitialContext context;
-
- @Override
- public void close() {
- instance.container.destroy();
- instance.container = null;
- }
-
- @Override
- public Context getContext() {
- return context;
- }
-
- public static class Provider implements EJBContainerProvider {
- private static final List<String> CONTAINER_NAMES = Arrays.asList(RemoteTomEEEJBContainer.class.getName(), "tomee-remote", "remote-tomee");
-
- @Override
- public EJBContainer createEJBContainer(final Map<?, ?> properties) {
- final Object provider = properties.get(EJBContainer.PROVIDER);
- int ejbContainerProviders = 1;
- try {
- ejbContainerProviders = ProviderLocator.getServices(EJBContainerProvider.class.getName(), EJBContainer.class, Thread.currentThread().getContextClassLoader()).size();
- } catch (final Exception e) {
- // no-op
- }
-
- if ((provider == null && ejbContainerProviders > 1)
- || (!RemoteTomEEEJBContainer.class.equals(provider)
- && !CONTAINER_NAMES.contains(String.valueOf(provider)))) {
- return null;
- }
-
- if (instance != null) {
- return instance;
- }
-
- final Object modules = properties.get(EJBContainer.MODULES);
-
- System.getProperties().putAll(properties);
- final File home = new File(System.getProperty("openejb.home", "doesn't exist"));
- if (!home.exists()) {
- throw new IllegalArgumentException("You need to set openejb.home");
- }
-
- final QuickServerXmlParser parser = QuickServerXmlParser.parse(new File(home, "conf/server.xml"));
- final String remoteEjb = System.getProperty(Context.PROVIDER_URL, "http://" + parser.host() + ":" + parser.http() + "/tomee/ejb");
-
- try {
- instance = new RemoteTomEEEJBContainer();
- instance.container = new RemoteServer();
- instance.container.setPortStartup(Integer.parseInt(parser.http()));
- instance.container.start();
- instance.context = new InitialContext(new Properties() {{
- setProperty(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
- setProperty(Context.PROVIDER_URL, remoteEjb);
- }});
-
- final Deployer deployer = Deployer.class.cast(instance.context.lookup("openejb/DeployerBusinessRemote"));
-
- if (modules instanceof File) {
- final File file = File.class.cast(modules);
- deployFile(deployer, file);
- } else if (modules instanceof String) {
- final String path = String.class.cast(modules);
- final File file = new File(path);
- deployFile(deployer, file);
- } else if (modules instanceof String[]) {
- for (final String path : (String[]) modules) {
- deployFile(deployer, new File(path));
- }
- } else if (modules instanceof File[]) {
- for (final File file : (File[]) modules) {
- deployFile(deployer, file);
- }
- } // else suppose already deployed
-
- return instance;
- } catch (final OpenEJBException e) {
- throw new EJBException(e);
- } catch (final MalformedURLException e) {
- throw new EJBException(e);
- } catch (final ValidationException ve) {
- throw ve;
- } catch (final Exception e) {
- if (e instanceof EJBException) {
- throw (EJBException) e;
- }
- throw new TomEERemoteEJBContainerException("initialization exception", e);
- }
- }
- }
-
- private static void deployFile(final Deployer deployer, final File file) throws IOException, OpenEJBException {
- if ("true".equalsIgnoreCase(System.getProperty(DeployerEjb.OPENEJB_USE_BINARIES, "false"))) {
- final Properties props = new Properties();
- final byte[] slurpBinaries = IO.slurp(file).getBytes();
- props.put(DeployerEjb.OPENEJB_VALUE_BINARIES, slurpBinaries);
- props.put(DeployerEjb.OPENEJB_PATH_BINARIES, file.getName());
- deployer.deploy(file.getAbsolutePath(), props);
- } else {
- deployer.deploy(file.getAbsolutePath());
- }
- }
-
- protected static class TomEERemoteEJBContainerException extends RuntimeException {
- protected TomEERemoteEJBContainerException(final String s, final Exception e) {
- super(s, e);
- }
- }
-}
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomee;
+
+import org.apache.geronimo.osgi.locator.ProviderLocator;
+import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.assembler.Deployer;
+import org.apache.openejb.assembler.DeployerEjb;
+import org.apache.openejb.client.RemoteInitialContextFactory;
+import org.apache.openejb.config.RemoteServer;
+import org.apache.openejb.loader.IO;
+import org.apache.tomee.util.QuickServerXmlParser;
+
+import javax.ejb.EJBException;
+import javax.ejb.embeddable.EJBContainer;
+import javax.ejb.spi.EJBContainerProvider;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.validation.ValidationException;
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+public class RemoteTomEEEJBContainer extends EJBContainer {
+ private static RemoteTomEEEJBContainer instance;
+ private RemoteServer container;
+ private InitialContext context;
+
+ @Override
+ public void close() {
+ instance.container.destroy();
+ instance.container = null;
+ }
+
+ @Override
+ public Context getContext() {
+ return context;
+ }
+
+ public static class Provider implements EJBContainerProvider {
+ private static final List<String> CONTAINER_NAMES = Arrays.asList(RemoteTomEEEJBContainer.class.getName(), "tomee-remote", "remote-tomee");
+
+ @Override
+ public EJBContainer createEJBContainer(final Map<?, ?> properties) {
+ final Object provider = properties.get(EJBContainer.PROVIDER);
+ int ejbContainerProviders = 1;
+ try {
+ ejbContainerProviders = ProviderLocator.getServices(EJBContainerProvider.class.getName(), EJBContainer.class, Thread.currentThread().getContextClassLoader()).size();
+ } catch (final Exception e) {
+ // no-op
+ }
+
+ if ((provider == null && ejbContainerProviders > 1)
+ || (!RemoteTomEEEJBContainer.class.equals(provider)
+ && !CONTAINER_NAMES.contains(String.valueOf(provider)))) {
+ return null;
+ }
+
+ if (instance != null) {
+ return instance;
+ }
+
+ final Object modules = properties.get(EJBContainer.MODULES);
+
+ System.getProperties().putAll(properties);
+ final File home = new File(System.getProperty("openejb.home", "doesn't exist"));
+ if (!home.exists()) {
+ throw new IllegalArgumentException("You need to set openejb.home");
+ }
+
+ final QuickServerXmlParser parser = QuickServerXmlParser.parse(new File(home, "conf/server.xml"));
+ final String remoteEjb = System.getProperty(Context.PROVIDER_URL, "http://" + parser.host() + ":" + parser.http() + "/tomee/ejb");
+
+ try {
+ instance = new RemoteTomEEEJBContainer();
+ instance.container = new RemoteServer();
+ instance.container.setPortStartup(Integer.parseInt(parser.http()));
+
+ try {
+ instance.container.start();
+ } catch (final Exception e) {
+ instance.container.destroy();
+ throw e;
+ }
+
+ instance.context = new InitialContext(new Properties() {{
+ setProperty(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
+ setProperty(Context.PROVIDER_URL, remoteEjb);
+ }});
+
+ final Deployer deployer = Deployer.class.cast(instance.context.lookup("openejb/DeployerBusinessRemote"));
+
+ if (modules instanceof File) {
+ final File file = File.class.cast(modules);
+ deployFile(deployer, file);
+ } else if (modules instanceof String) {
+ final String path = String.class.cast(modules);
+ final File file = new File(path);
+ deployFile(deployer, file);
+ } else if (modules instanceof String[]) {
+ for (final String path : (String[]) modules) {
+ deployFile(deployer, new File(path));
+ }
+ } else if (modules instanceof File[]) {
+ for (final File file : (File[]) modules) {
+ deployFile(deployer, file);
+ }
+ } // else suppose already deployed
+
+ return instance;
+ } catch (final OpenEJBException e) {
+ throw new EJBException(e);
+ } catch (final MalformedURLException e) {
+ throw new EJBException(e);
+ } catch (final ValidationException ve) {
+ throw ve;
+ } catch (final Exception e) {
+ if (e instanceof EJBException) {
+ throw (EJBException) e;
+ }
+ throw new TomEERemoteEJBContainerException("initialization exception", e);
+ }
+ }
+ }
+
+ private static void deployFile(final Deployer deployer, final File file) throws IOException, OpenEJBException {
+ if ("true".equalsIgnoreCase(System.getProperty(DeployerEjb.OPENEJB_USE_BINARIES, "false"))) {
+ final Properties props = new Properties();
+ final byte[] slurpBinaries = IO.slurp(file).getBytes();
+ props.put(DeployerEjb.OPENEJB_VALUE_BINARIES, slurpBinaries);
+ props.put(DeployerEjb.OPENEJB_PATH_BINARIES, file.getName());
+ deployer.deploy(file.getAbsolutePath(), props);
+ } else {
+ deployer.deploy(file.getAbsolutePath());
+ }
+ }
+
+ protected static class TomEERemoteEJBContainerException extends RuntimeException {
+ protected TomEERemoteEJBContainerException(final String s, final Exception e) {
+ super(s, e);
+ }
+ }
+}
[2/3] tomee git commit: Delegate RemoteServer.destroy() to usage -
Try/catch
Posted by an...@apache.org.
http://git-wip-us.apache.org/repos/asf/tomee/blob/6df9839b/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java b/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java
index 891833e..9f7da52 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java
@@ -1,704 +1,714 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.openejb.config;
-
-import org.apache.openejb.OpenEJBRuntimeException;
-import org.apache.openejb.loader.Options;
-import org.apache.openejb.util.Join;
-import org.apache.openejb.util.Pipe;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.lang.reflect.Field;
-import java.net.InetSocketAddress;
-import java.net.Socket;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicReference;
-
-//import org.apache.openejb.loader.IO;
-
-/**
- * NOTE: Do not add inner or anonymous classes or a dependency without updating ExecMojo
- *
- * @version $Rev$ $Date$
- */
-@SuppressWarnings("UseOfSystemOutOrSystemErr")
-public class RemoteServer {
-
- private static final Options options = new Options(System.getProperties());
- public static final String SERVER_DEBUG_PORT = "server.debug.port";
- public static final String SERVER_SHUTDOWN_PORT = "server.shutdown.port";
- public static final String SERVER_SHUTDOWN_HOST = "server.shutdown.host";
- public static final String SERVER_SHUTDOWN_COMMAND = "server.shutdown.command";
- public static final String OPENEJB_SERVER_DEBUG = "openejb.server.debug";
- public static final String START = "start";
- public static final String STOP = "stop";
-
- private final boolean debug = options.get(OPENEJB_SERVER_DEBUG, false);
- private final boolean profile = options.get("openejb.server.profile", false);
- private final boolean tomcat;
- private final String javaOpts = System.getProperty("java.opts");
- private String additionalClasspath;
-
- /**
- * Has the remote server's instance been already running ?
- */
- private boolean serverHasAlreadyBeenStarted = true;
-
- private Properties properties;
- private final AtomicReference<Process> server = new AtomicReference<Process>();
- private final int tries;
- private final boolean verbose;
- private final int portShutdown;
- private final String host;
- private final String command;
- private File home;
- private int portStartup;
-
- public RemoteServer() {
- this(options.get("connect.tries", 60), options.get("verbose", false));
- }
-
- public RemoteServer(final int tries, final boolean verbose) {
- this.tries = (tries < 1 ? 1 : (tries > 3600 ? 3600 : tries)); //Wait at least 1 second to start or stop, but not more than an hour.
- this.verbose = verbose;
- home = getHome();
- tomcat = (home != null) && (new File(new File(home, "bin"), "catalina.sh").exists());
-
- portShutdown = options.get(SERVER_SHUTDOWN_PORT, tomcat ? 8005 : 4200);
- portStartup = portShutdown;
- command = options.get(SERVER_SHUTDOWN_COMMAND, "SHUTDOWN");
- host = options.get(SERVER_SHUTDOWN_HOST, "localhost");
- }
-
- public void init(final Properties props) {
- properties = props;
-
- props.put("java.naming.factory.initial", "org.apache.openejb.client.RemoteInitialContextFactory");
- final int port = options.get("ejbd.port", 4201);
- props.put("java.naming.provider.url", options.get("java.naming.provider.url", "127.0.0.1:" + port));
- props.put("java.naming.security.principal", "testuser");
- props.put("java.naming.security.credentials", "testpassword");
- }
-
- public static void main(final String[] args) {
- assert args.length > 0 : "no arguments supplied: valid arguments are 'start' or 'stop'";
- if (args[0].equalsIgnoreCase(START)) {
- new RemoteServer().start();
- } else if (args[0].equalsIgnoreCase(STOP)) {
- final RemoteServer remoteServer = new RemoteServer();
- remoteServer.serverHasAlreadyBeenStarted = false;
- try {
- remoteServer.forceStop();
- } catch (final Exception e) {
- e.printStackTrace(System.err);
- }
- } else {
- throw new OpenEJBRuntimeException("valid arguments are 'start' or 'stop'");
- }
- }
-
- public int getPortStartup() {
- return this.portStartup;
- }
-
- public void setPortStartup(final int portStartup) {
- this.portStartup = portStartup;
- }
-
- public Properties getProperties() {
- return this.properties;
- }
-
- public void destroy() {
-
- final boolean stopSent = stop();
-
- final Process p = server.get();
- if (p != null) {
-
- if (stopSent) {
- waitFor(p);
- } else {
- p.destroy();
- }
- }
- }
-
- public void start() {
- start(Collections.<String>emptyList(), START, true);
- }
-
- public void start(final List<String> additionalArgs, final String cmd, final boolean checkPortAvailable) {
- cmd(additionalArgs, cmd, checkPortAvailable);
- }
-
- private void cmd(final List<String> additionalArgs, final String cmd, final boolean checkPortAvailable) {
- boolean ok = true;
- final int port = START.equals(cmd) ? portStartup : portShutdown;
-
- if (checkPortAvailable) {
- ok = !connect(port, 1);
- }
-
- if (ok) {
- try {
- if (verbose) {
- System.out.println("[] " + cmd.toUpperCase() + " SERVER");
- }
-
- final File home = getHome();
- final String javaVersion = System.getProperty("java.version");
- if (verbose) {
- System.out.println("OPENEJB_HOME = " + home.getAbsolutePath());
- final String systemInfo = "Java " + javaVersion + "; " + System.getProperty("os.name") + "/" + System.getProperty("os.version");
- System.out.println("SYSTEM_INFO = " + systemInfo);
- }
-
- serverHasAlreadyBeenStarted = false;
-
- final File lib = new File(home, "lib");
- final File webapplib = new File(new File(new File(home, "webapps"), "tomee"), "lib");
-
- File javaagentJar = null;
- try {
- javaagentJar = lib("openejb-javaagent", lib, webapplib);
- } catch (final IllegalStateException ise) {
- // no-op
- }
-
- final File conf = new File(home, "conf");
- final File loggingProperties = new File(conf, "logging.properties");
-
- //File openejbJar = new File(lib, "openejb-core-" + version + ".jar");
-
- final String java;
- final boolean isWindows = System.getProperty("os.name", "unknown").toLowerCase().startsWith("windows");
- if (isWindows && START.equals(cmd) && options.get("server.windows.fork", false)) {
- // run and forget
- java = new File(System.getProperty("java.home"), "bin/javaw").getAbsolutePath();
- } else {
- java = new File(System.getProperty("java.home"), "bin/java").getAbsolutePath();
- }
-
- final List<String> argsList = new ArrayList<String>(20);
- argsList.add(java);
- argsList.add("-XX:+HeapDumpOnOutOfMemoryError");
-
- if (debug) {
- argsList.add("-Xdebug");
- argsList.add("-Xnoagent");
- argsList.add("-Djava.compiler=NONE");
- argsList.add("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=" + options.get(SERVER_DEBUG_PORT, 5005));
- }
-
- if (profile) {
- String yourkitHome = options.get("yourkit.home", "/Applications/YourKit_Java_Profiler_9.5.6.app/bin/mac/");
- if (!yourkitHome.endsWith("/")) {
- yourkitHome += "/";
- }
- final String yourkitOpts = options.get("yourkit.opts", "disablestacktelemetry,disableexceptiontelemetry,builtinprobes=none,delay=10000,sessionname=Tomcat");
- argsList.add("-agentpath:" + yourkitHome + "libyjpagent.jnilib=" + yourkitOpts);
- }
-
- if (javaOpts != null) {
- Collections.addAll(argsList, javaOpts.split(" +"));
- }
-
- final Map<String, String> addedArgs = new HashMap<String, String>();
- if (additionalArgs != null) {
- for (final String arg : additionalArgs) {
- final String[] values = arg.split("=");
- if (values.length == 1) {
- addedArgs.put(values[0], "null");
- } else {
- addedArgs.put(values[0], values[1]);
- }
- argsList.add(arg);
- }
- }
-
- if (!addedArgs.containsKey("-Djava.util.logging.config.file") && loggingProperties.exists()) {
- argsList.add("-Djava.util.logging.config.file=" + loggingProperties.getAbsolutePath());
- }
-
- if (javaagentJar != null && javaagentJar.exists()) {
- argsList.add("-javaagent:" + javaagentJar.getAbsolutePath());
- }
-
- //DMB: If you don't use an array, you get problems with jar paths containing spaces
- // the command won't parse correctly
- final String ps = File.pathSeparator;
-
- final String[] args;
- if (!tomcat) {
- final File openejbJar = lib("openejb-core", lib, webapplib);
- final StringBuilder cp = new StringBuilder(openejbJar.getAbsolutePath());
- if (additionalClasspath != null) {
- cp.append(ps).append(additionalClasspath);
- }
-
- argsList.add("-cp");
- argsList.add(cp.toString());
- argsList.add("org.apache.openejb.cli.Bootstrap");
- } else {
- final File bin = new File(home, "bin");
- final File tlib = new File(home, "lib");
- final File bootstrapJar = new File(bin, "bootstrap.jar");
- final File juliJar = new File(bin, "tomcat-juli.jar");
- final File commonsLoggingJar = new File(bin, "commons-logging-api.jar");
-
- final File endorsed = new File(home, "endorsed");
- final File temp = new File(home, "temp");
-
-// if (!addedArgs.containsKey("-Dcom.sun.management.jmxremote")) {
-// argsList.add("-Dcom.sun.management.jmxremote");
-// }
- if (!addedArgs.containsKey("-Djava.util.logging.manager")) {
- argsList.add("-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager");
- }
- if (!addedArgs.containsKey("-Djava.io.tmpdir")) {
- argsList.add("-Djava.io.tmpdir=" + temp.getAbsolutePath());
- }
- if (!addedArgs.containsKey("-Djava.endorsed.dirs")) {
- argsList.add("-Djava.endorsed.dirs=" + endorsed.getAbsolutePath());
- }
- if (!addedArgs.containsKey("-Dcatalina.base")) {
- argsList.add("-Dcatalina.base=" + home.getAbsolutePath());
- }
- if (!addedArgs.containsKey("-Dcatalina.home")) {
- argsList.add("-Dcatalina.home=" + home.getAbsolutePath());
- }
- if (!addedArgs.containsKey("-Dcatalina.ext.dirs")) {
- argsList.add("-Dcatalina.ext.dirs=" + tlib.getAbsolutePath());
- }
- if (!addedArgs.containsKey("-Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE")) {
- argsList.add("-Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=true");
- }
- if (!addedArgs.containsKey("-Dorg.apache.tomcat.util.http.ServerCookie.ALLOW_HTTP_SEPARATORS_IN_V0")) {
- argsList.add("-Dorg.apache.tomcat.util.http.ServerCookie.ALLOW_HTTP_SEPARATORS_IN_V0=true");
- }
-
- if (addedArgs.isEmpty()) { // default case
- addIfSet(argsList, "javax.net.ssl.keyStore");
- addIfSet(argsList, "javax.net.ssl.keyStorePassword");
- addIfSet(argsList, "javax.net.ssl.trustStore");
- addIfSet(argsList, "java.protocol.handler.pkgs");
- }
-
- argsList.add("-ea");
- argsList.add("-classpath");
-
- final StringBuilder cp = new StringBuilder(bootstrapJar.getAbsolutePath()).append(ps).append(juliJar.getAbsolutePath());
- if (commonsLoggingJar.exists()) {
- cp.append(ps).append(commonsLoggingJar.getAbsolutePath());
- }
- if (additionalClasspath != null) {
- cp.append(ps).append(additionalClasspath);
- }
- argsList.add(cp.toString());
-
- argsList.add("org.apache.catalina.startup.Bootstrap");
- }
-
- if (cmd == null) {
- argsList.add(START);
- } else {
- argsList.add(cmd);
- }
- args = argsList.toArray(new String[argsList.size()]);
-
- if (verbose) {
- System.out.println(Join.join("\n", args));
- }
-
- // kill3UNIXDebug();
- final ProcessBuilder pb = new ProcessBuilder(args);
- pb.directory(home.getAbsoluteFile());
- Process p = pb.start();
-
- //Process p = Runtime.getRuntime().exec(args);
- Pipe.pipeOut(p); // why would we need to redirect System.in to the process, TomEE doesn't use it
-
- if (START.equals(cmd)) {
- server.set(p);
- } else if (STOP.equals(cmd)) {
- waitFor(p);
- p = server.get();
- if (p != null) {
- waitFor(p);
- }
- }
-
- System.out.println("Started server process on port: " + port);
-
- } catch (final Exception e) {
- throw (RuntimeException) new OpenEJBRuntimeException("Cannot start the server. Exception: " + e.getClass().getName() + ": " + e.getMessage()).initCause(e);
- }
-
- if (debug) {
- if (!connect(port, Integer.MAX_VALUE)) {
- destroy();
- throw new OpenEJBRuntimeException("Could not connect to server");
- }
- } else {
- if (!connect(port, tries)) {
- destroy();
- throw new OpenEJBRuntimeException("Could not connect to server");
- }
- }
-
- } else {
- if (verbose) {
- System.out.println("[] FOUND STARTED SERVER");
- }
- }
- }
-
- private void waitFor(final Process p) {
- final CountDownLatch latch = new CountDownLatch(1);
- final Thread t = new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- p.waitFor();
- } catch (final InterruptedException e) {
- //Ignore
- }
-
- latch.countDown();
- }
- }, "process-waitFor");
-
- t.start();
-
- try {
- if (!latch.await(10, TimeUnit.SECONDS)) {
- killOnExit(p);
- throw new RuntimeException("Timeout waiting for process");
- }
- } catch (final InterruptedException e) {
- killOnExit(p);
- }
- }
-
- public void kill3UNIX() { // debug purpose only
- if (System.getProperty("os.name", "unknown").toLowerCase().startsWith("windows")) {
- return;
- }
-
- try {
- final Field f = server.get().getClass().getDeclaredField("pid");
- f.setAccessible(true);
- final int pid = (Integer) f.get(server.get());
- Pipe.pipe(Runtime.getRuntime().exec("kill -3 " + pid));
- } catch (final Exception e1) {
- e1.printStackTrace();
- }
- }
-
- private File lib(final String name, final File... dirs) {
- for (final File dir : dirs) {
- final File[] files = dir.listFiles();
- if (files != null) {
- for (final File file : files) {
- if (!file.isFile()) {
- continue;
- }
- if (!file.getName().endsWith(".jar")) {
- continue;
- }
- if (file.getName().startsWith(name)) {
- return file;
- }
- }
- }
- }
-
- if (debug) {
- for (final File dir : dirs) {
- dumpLibs(dir);
- }
- }
- throw new IllegalStateException("Cannot find the " + name + " jar");
- }
-
- // for debug purpose
- private static void dumpLibs(final File dir) {
- if (!dir.exists()) {
- System.out.println("lib dir doesn't exist");
- return;
- }
- final File[] files = dir.listFiles();
- if (files != null) {
- for (final File lib : files) {
- System.out.println(lib.getAbsolutePath());
- }
- }
- }
-
- public Process getServer() {
- return server.get();
- }
-
- private void addIfSet(final List<String> argsList, final String key) {
- if (System.getProperties().containsKey(key)) {
- argsList.add("-D" + key + "=" + System.getProperty(key));
- }
- }
-
- private File getHome() {
- if (home != null) {
- return home;
- }
-
- final String openejbHome = System.getProperty("openejb.home");
-
- if (openejbHome != null) {
- home = new File(openejbHome);
- }
- return home;
- }
-
- public boolean stop() {
- if (sendShutdown(5)) {
- return true;
- } else {
- if (verbose) {
- notSent();
- }
-
- return false;
- }
- }
-
- private void notSent() {
- System.out.println("Failed to send the shutdown notification - TomEE is likely shut down already");
- }
-
- public void forceStop() throws Exception {
- if (sendShutdown(5)) {
-
- // Check TomEE was effectively shut down after getting the message
- // There can be concurrent shutdown operations (catalina shutdown hook for instance),
- // so we have to wait here since it is important to be synchronous in this method
- waitForServerShutdown();
- } else {
- if (verbose) {
- notSent();
- }
- }
- }
-
- private void waitForServerShutdown() throws Exception {
-
- if (verbose) {
- System.out.print("Waiting for TomEE shutdown.");
- }
-
- final boolean b = disconnect(portShutdown, tries);
-
- if (verbose) {
- System.out.println();
- }
-
- if (!b) {
- //We need to know about this
- System.out.println("SEVERE: Failed to shutdown TomEE running on port " + portStartup + " using shutdown port: " + portShutdown);
- }
- }
-
- /**
- * Send the shutdown message to the running server
- *
- * @param attempts How many times to try to send the message before giving up
- * @return True is the message was sent, else false if unable to connect after the defined number of attempts
- */
- private boolean sendShutdown(int attempts) {
- Socket socket = null;
- OutputStream stream = null;
- try {
- socket = new Socket(host, portShutdown);
- stream = socket.getOutputStream();
- final String shutdown = command + Character.toString((char) 0);
- for (int i = 0; i < shutdown.length(); i++) {
- stream.write(shutdown.charAt(i));
- }
- stream.flush();
- } catch (final Exception e) {
- if (attempts > 0) {
- try {
- Thread.sleep(1000);
- } catch (final InterruptedException ie) {
- return false;
- }
- return sendShutdown(--attempts);
- } else {
- return false;
- }
- } finally {
- if (null != stream) {
- try {
- stream.close();
- } catch (final Exception e) {
- // Ignore
- }
- }
- if (socket != null) {
- try {
- socket.close();
- } catch (final Exception e) {
- // Ignore
- }
- }
- }
-
- return true;
- }
-
- private boolean connect(final int port, int tries) {
- if (verbose) {
- System.out.println("[] CONNECT ATTEMPT " + (this.tries - tries) + " on port: " + port);
- }
-
- Socket s = null;
- try {
- s = new Socket();
- s.connect(new InetSocketAddress(this.host, port), 1000);
- s.getOutputStream().close();
- if (verbose) {
- System.out.println("[] CONNECTED IN " + (this.tries - tries));
- }
- } catch (final Exception e) {
- if (tries < 2) {
- if (verbose) {
- System.out.println("[] CONNECT ATTEMPTS FAILED ( " + (this.tries - tries) + " ATTEMPTS)");
- }
- return false;
- } else {
- try {
- Thread.sleep(1000);
- } catch (final Exception e2) {
- e2.printStackTrace();
- }
- return connect(port, --tries);
- }
- } finally {
- if (s != null) {
- try {
- s.close();
- } catch (final Exception ignored) {
- // no-op
- }
- }
- }
-
- return true;
- }
-
- private boolean disconnect(final int port, int tries) {
- if (verbose) {
- System.out.println("[] DISCONNECT ATTEMPT " + (this.tries - tries) + " on port: " + port);
- }
-
- Socket s = null;
- try {
- s = new Socket();
- s.connect(new InetSocketAddress(this.host, port), 500);
- s.getOutputStream().close();
-
- if (verbose) {
- System.out.println("[] NOT DISCONNECTED AFTER ( " + (this.tries - tries) + " ATTEMPTS)");
- }
-
- if (tries < 2) {
- //Give up
- return false;
- } else {
- try {
- Thread.sleep(1000);
- } catch (final Exception e2) {
- e2.printStackTrace();
- }
-
- return disconnect(port, --tries);
- }
-
- } catch (final IOException e) {
- //This is what we want
- } finally {
- if (s != null) {
- try {
- s.close();
- } catch (final Exception ignored) {
- // no-op
- }
- }
- }
-
- return true;
- }
-
- public void setAdditionalClasspath(final String additionalClasspath) {
- this.additionalClasspath = additionalClasspath;
- }
-
- public void killOnExit() {
- final Process p = this.server.get();
- if (!serverHasAlreadyBeenStarted && kill.contains(p)) {
- return;
- }
-
- killOnExit(p);
- }
-
- private static void killOnExit(final Process p) {
- kill.add(p);
- }
-
- // Shutdown hook for processes
- private static final List<Process> kill = new ArrayList<Process>();
-
- static {
- Runtime.getRuntime().addShutdownHook(new CleanUpThread());
- }
-
- public static class CleanUpThread extends Thread {
- @Override
- public void run() {
- for (final Process server : kill) {
- try {
- if (server != null) {
- server.destroy();
- server.waitFor();
- }
- } catch (final Throwable e) {
- //Ignore
- }
- }
- }
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.openejb.config;
+
+import org.apache.openejb.OpenEJBRuntimeException;
+import org.apache.openejb.loader.Options;
+import org.apache.openejb.util.Join;
+import org.apache.openejb.util.Pipe;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.reflect.Field;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+//import org.apache.openejb.loader.IO;
+
+/**
+ * NOTE: Do not add inner or anonymous classes or a dependency without updating ExecMojo
+ *
+ * @version $Rev$ $Date$
+ */
+@SuppressWarnings("UseOfSystemOutOrSystemErr")
+public class RemoteServer {
+
+ private static final Options options = new Options(System.getProperties());
+ public static final String SERVER_DEBUG_PORT = "server.debug.port";
+ public static final String SERVER_SHUTDOWN_PORT = "server.shutdown.port";
+ public static final String SERVER_SHUTDOWN_HOST = "server.shutdown.host";
+ public static final String SERVER_SHUTDOWN_COMMAND = "server.shutdown.command";
+ public static final String OPENEJB_SERVER_DEBUG = "openejb.server.debug";
+ public static final String START = "start";
+ public static final String STOP = "stop";
+
+ private final boolean debug = options.get(OPENEJB_SERVER_DEBUG, false);
+ private final boolean profile = options.get("openejb.server.profile", false);
+ private final boolean tomcat;
+ private final String javaOpts = System.getProperty("java.opts");
+ private String additionalClasspath;
+
+ /**
+ * Has the remote server's instance been already running ?
+ */
+ private boolean serverHasAlreadyBeenStarted = true;
+
+ private Properties properties;
+ private final AtomicReference<Process> server = new AtomicReference<Process>();
+ private final int tries;
+ private final boolean verbose;
+ private final int portShutdown;
+ private final String host;
+ private final String command;
+ private File home;
+ private int portStartup;
+
+ public RemoteServer() {
+ this(options.get("connect.tries", 60), options.get("verbose", false));
+ }
+
+ public RemoteServer(final int tries, final boolean verbose) {
+ this.tries = (tries < 1 ? 1 : (tries > 3600 ? 3600 : tries)); //Wait at least 1 second to start or stop, but not more than an hour.
+ this.verbose = verbose;
+ home = getHome();
+ tomcat = (home != null) && (new File(new File(home, "bin"), "catalina.sh").exists());
+
+ portShutdown = options.get(SERVER_SHUTDOWN_PORT, tomcat ? 8005 : 4200);
+ portStartup = portShutdown;
+ command = options.get(SERVER_SHUTDOWN_COMMAND, "SHUTDOWN");
+ host = options.get(SERVER_SHUTDOWN_HOST, "localhost");
+ }
+
+ public void init(final Properties props) {
+ properties = props;
+
+ props.put("java.naming.factory.initial", "org.apache.openejb.client.RemoteInitialContextFactory");
+ final int port = options.get("ejbd.port", 4201);
+ props.put("java.naming.provider.url", options.get("java.naming.provider.url", "127.0.0.1:" + port));
+ props.put("java.naming.security.principal", "testuser");
+ props.put("java.naming.security.credentials", "testpassword");
+ }
+
+ public static void main(final String[] args) {
+ assert args.length > 0 : "no arguments supplied: valid arguments are 'start' or 'stop'";
+ if (args[0].equalsIgnoreCase(START)) {
+ final RemoteServer remoteServer = new RemoteServer();
+ try {
+ remoteServer.start();
+ } catch (final Exception e) {
+ remoteServer.destroy();
+ e.printStackTrace(System.err);
+ }
+ } else if (args[0].equalsIgnoreCase(STOP)) {
+ final RemoteServer remoteServer = new RemoteServer();
+ remoteServer.serverHasAlreadyBeenStarted = false;
+ try {
+ remoteServer.forceStop();
+ } catch (final Exception e) {
+ e.printStackTrace(System.err);
+ }
+ } else {
+ throw new OpenEJBRuntimeException("valid arguments are 'start' or 'stop'");
+ }
+ }
+
+ public int getPortStartup() {
+ return this.portStartup;
+ }
+
+ public void setPortStartup(final int portStartup) {
+ this.portStartup = portStartup;
+ }
+
+ public Properties getProperties() {
+ return this.properties;
+ }
+
+ public void destroy() {
+
+ try {
+ final boolean stopSent = stop();
+
+ final Process p = server.get();
+ if (p != null) {
+
+ if (stopSent) {
+ waitFor(p);
+ } else {
+ p.destroy();
+ }
+ }
+ } catch (final Exception e) {
+ Logger.getLogger(RemoteServer.class.getName()).log(Level.WARNING, "Failed to destroy remote server process", e);
+ }
+ }
+
+ public void start() {
+ start(Collections.<String>emptyList(), START, true);
+ }
+
+ public void start(final List<String> additionalArgs, final String cmd, final boolean checkPortAvailable) {
+ cmd(additionalArgs, cmd, checkPortAvailable);
+ }
+
+ private void cmd(final List<String> additionalArgs, final String cmd, final boolean checkPortAvailable) {
+ boolean ok = true;
+ final int port = START.equals(cmd) ? portStartup : portShutdown;
+
+ if (checkPortAvailable) {
+ ok = !connect(port, 1);
+ }
+
+ if (ok) {
+ try {
+ if (verbose) {
+ System.out.println("[] " + cmd.toUpperCase() + " SERVER");
+ }
+
+ final File home = getHome();
+ final String javaVersion = System.getProperty("java.version");
+ if (verbose) {
+ System.out.println("OPENEJB_HOME = " + home.getAbsolutePath());
+ final String systemInfo = "Java " + javaVersion + "; " + System.getProperty("os.name") + "/" + System.getProperty("os.version");
+ System.out.println("SYSTEM_INFO = " + systemInfo);
+ }
+
+ serverHasAlreadyBeenStarted = false;
+
+ final File lib = new File(home, "lib");
+ final File webapplib = new File(new File(new File(home, "webapps"), "tomee"), "lib");
+
+ File javaagentJar = null;
+ try {
+ javaagentJar = lib("openejb-javaagent", lib, webapplib);
+ } catch (final IllegalStateException ise) {
+ // no-op
+ }
+
+ final File conf = new File(home, "conf");
+ final File loggingProperties = new File(conf, "logging.properties");
+
+ //File openejbJar = new File(lib, "openejb-core-" + version + ".jar");
+
+ final String java;
+ final boolean isWindows = System.getProperty("os.name", "unknown").toLowerCase().startsWith("windows");
+ if (isWindows && START.equals(cmd) && options.get("server.windows.fork", false)) {
+ // run and forget
+ java = new File(System.getProperty("java.home"), "bin/javaw").getAbsolutePath();
+ } else {
+ java = new File(System.getProperty("java.home"), "bin/java").getAbsolutePath();
+ }
+
+ final List<String> argsList = new ArrayList<String>(20);
+ argsList.add(java);
+ argsList.add("-XX:+HeapDumpOnOutOfMemoryError");
+
+ if (debug) {
+ argsList.add("-Xdebug");
+ argsList.add("-Xnoagent");
+ argsList.add("-Djava.compiler=NONE");
+ argsList.add("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=" + options.get(SERVER_DEBUG_PORT, 5005));
+ }
+
+ if (profile) {
+ String yourkitHome = options.get("yourkit.home", "/Applications/YourKit_Java_Profiler_9.5.6.app/bin/mac/");
+ if (!yourkitHome.endsWith("/")) {
+ yourkitHome += "/";
+ }
+ final String yourkitOpts = options.get("yourkit.opts", "disablestacktelemetry,disableexceptiontelemetry,builtinprobes=none,delay=10000,sessionname=Tomcat");
+ argsList.add("-agentpath:" + yourkitHome + "libyjpagent.jnilib=" + yourkitOpts);
+ }
+
+ if (javaOpts != null) {
+ Collections.addAll(argsList, javaOpts.split(" +"));
+ }
+
+ final Map<String, String> addedArgs = new HashMap<String, String>();
+ if (additionalArgs != null) {
+ for (final String arg : additionalArgs) {
+ final String[] values = arg.split("=");
+ if (values.length == 1) {
+ addedArgs.put(values[0], "null");
+ } else {
+ addedArgs.put(values[0], values[1]);
+ }
+ argsList.add(arg);
+ }
+ }
+
+ if (!addedArgs.containsKey("-Djava.util.logging.config.file") && loggingProperties.exists()) {
+ argsList.add("-Djava.util.logging.config.file=" + loggingProperties.getAbsolutePath());
+ }
+
+ if (javaagentJar != null && javaagentJar.exists()) {
+ argsList.add("-javaagent:" + javaagentJar.getAbsolutePath());
+ }
+
+ //DMB: If you don't use an array, you get problems with jar paths containing spaces
+ // the command won't parse correctly
+ final String ps = File.pathSeparator;
+
+ final String[] args;
+ if (!tomcat) {
+ final File openejbJar = lib("openejb-core", lib, webapplib);
+ final StringBuilder cp = new StringBuilder(openejbJar.getAbsolutePath());
+ if (additionalClasspath != null) {
+ cp.append(ps).append(additionalClasspath);
+ }
+
+ argsList.add("-cp");
+ argsList.add(cp.toString());
+ argsList.add("org.apache.openejb.cli.Bootstrap");
+ } else {
+ final File bin = new File(home, "bin");
+ final File tlib = new File(home, "lib");
+ final File bootstrapJar = new File(bin, "bootstrap.jar");
+ final File juliJar = new File(bin, "tomcat-juli.jar");
+ final File commonsLoggingJar = new File(bin, "commons-logging-api.jar");
+
+ final File endorsed = new File(home, "endorsed");
+ final File temp = new File(home, "temp");
+
+// if (!addedArgs.containsKey("-Dcom.sun.management.jmxremote")) {
+// argsList.add("-Dcom.sun.management.jmxremote");
+// }
+ if (!addedArgs.containsKey("-Djava.util.logging.manager")) {
+ argsList.add("-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager");
+ }
+ if (!addedArgs.containsKey("-Djava.io.tmpdir")) {
+ argsList.add("-Djava.io.tmpdir=" + temp.getAbsolutePath());
+ }
+ if (!addedArgs.containsKey("-Djava.endorsed.dirs")) {
+ argsList.add("-Djava.endorsed.dirs=" + endorsed.getAbsolutePath());
+ }
+ if (!addedArgs.containsKey("-Dcatalina.base")) {
+ argsList.add("-Dcatalina.base=" + home.getAbsolutePath());
+ }
+ if (!addedArgs.containsKey("-Dcatalina.home")) {
+ argsList.add("-Dcatalina.home=" + home.getAbsolutePath());
+ }
+ if (!addedArgs.containsKey("-Dcatalina.ext.dirs")) {
+ argsList.add("-Dcatalina.ext.dirs=" + tlib.getAbsolutePath());
+ }
+ if (!addedArgs.containsKey("-Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE")) {
+ argsList.add("-Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=true");
+ }
+ if (!addedArgs.containsKey("-Dorg.apache.tomcat.util.http.ServerCookie.ALLOW_HTTP_SEPARATORS_IN_V0")) {
+ argsList.add("-Dorg.apache.tomcat.util.http.ServerCookie.ALLOW_HTTP_SEPARATORS_IN_V0=true");
+ }
+
+ if (addedArgs.isEmpty()) { // default case
+ addIfSet(argsList, "javax.net.ssl.keyStore");
+ addIfSet(argsList, "javax.net.ssl.keyStorePassword");
+ addIfSet(argsList, "javax.net.ssl.trustStore");
+ addIfSet(argsList, "java.protocol.handler.pkgs");
+ }
+
+ argsList.add("-ea");
+ argsList.add("-classpath");
+
+ final StringBuilder cp = new StringBuilder(bootstrapJar.getAbsolutePath()).append(ps).append(juliJar.getAbsolutePath());
+ if (commonsLoggingJar.exists()) {
+ cp.append(ps).append(commonsLoggingJar.getAbsolutePath());
+ }
+ if (additionalClasspath != null) {
+ cp.append(ps).append(additionalClasspath);
+ }
+ argsList.add(cp.toString());
+
+ argsList.add("org.apache.catalina.startup.Bootstrap");
+ }
+
+ if (cmd == null) {
+ argsList.add(START);
+ } else {
+ argsList.add(cmd);
+ }
+ args = argsList.toArray(new String[argsList.size()]);
+
+ if (verbose) {
+ System.out.println(Join.join("\n", args));
+ }
+
+ // kill3UNIXDebug();
+ final ProcessBuilder pb = new ProcessBuilder(args);
+ pb.directory(home.getAbsoluteFile());
+ Process p = pb.start();
+
+ //Process p = Runtime.getRuntime().exec(args);
+ Pipe.pipeOut(p); // why would we need to redirect System.in to the process, TomEE doesn't use it
+
+ if (START.equals(cmd)) {
+ server.set(p);
+ } else if (STOP.equals(cmd)) {
+ waitFor(p);
+ p = server.get();
+ if (p != null) {
+ waitFor(p);
+ }
+ }
+
+ System.out.println("Started server process on port: " + port);
+
+ } catch (final Exception e) {
+ throw (RuntimeException) new OpenEJBRuntimeException("Cannot start the server. Exception: " + e.getClass().getName() + ": " + e.getMessage()).initCause(e);
+ }
+
+ if (debug) {
+ if (!connect(port, Integer.MAX_VALUE)) {
+ throw new OpenEJBRuntimeException("Could not connect to server: " + this.host + ":" + port);
+ }
+ } else {
+ if (!connect(port, tries)) {
+ throw new OpenEJBRuntimeException("Could not connect to server: " + this.host + ":" + port);
+ }
+ }
+
+ } else {
+ if (verbose) {
+ System.out.println("[] FOUND STARTED SERVER");
+ }
+ }
+ }
+
+ private void waitFor(final Process p) {
+ final CountDownLatch latch = new CountDownLatch(1);
+ final Thread t = new Thread(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ p.waitFor();
+ } catch (final InterruptedException e) {
+ //Ignore
+ }
+
+ latch.countDown();
+ }
+ }, "process-waitFor");
+
+ t.start();
+
+ try {
+ if (!latch.await(10, TimeUnit.SECONDS)) {
+ killOnExit(p);
+ throw new RuntimeException("Timeout waiting for process");
+ }
+ } catch (final InterruptedException e) {
+ killOnExit(p);
+ }
+ }
+
+ public void kill3UNIX() { // debug purpose only
+ if (System.getProperty("os.name", "unknown").toLowerCase().startsWith("windows")) {
+ return;
+ }
+
+ try {
+ final Field f = server.get().getClass().getDeclaredField("pid");
+ f.setAccessible(true);
+ final int pid = (Integer) f.get(server.get());
+ Pipe.pipe(Runtime.getRuntime().exec("kill -3 " + pid));
+ } catch (final Exception e1) {
+ e1.printStackTrace();
+ }
+ }
+
+ private File lib(final String name, final File... dirs) {
+ for (final File dir : dirs) {
+ final File[] files = dir.listFiles();
+ if (files != null) {
+ for (final File file : files) {
+ if (!file.isFile()) {
+ continue;
+ }
+ if (!file.getName().endsWith(".jar")) {
+ continue;
+ }
+ if (file.getName().startsWith(name)) {
+ return file;
+ }
+ }
+ }
+ }
+
+ if (debug) {
+ for (final File dir : dirs) {
+ dumpLibs(dir);
+ }
+ }
+ throw new IllegalStateException("Cannot find the " + name + " jar");
+ }
+
+ // for debug purpose
+ private static void dumpLibs(final File dir) {
+ if (!dir.exists()) {
+ System.out.println("lib dir doesn't exist");
+ return;
+ }
+ final File[] files = dir.listFiles();
+ if (files != null) {
+ for (final File lib : files) {
+ System.out.println(lib.getAbsolutePath());
+ }
+ }
+ }
+
+ public Process getServer() {
+ return server.get();
+ }
+
+ private void addIfSet(final List<String> argsList, final String key) {
+ if (System.getProperties().containsKey(key)) {
+ argsList.add("-D" + key + "=" + System.getProperty(key));
+ }
+ }
+
+ private File getHome() {
+ if (home != null) {
+ return home;
+ }
+
+ final String openejbHome = System.getProperty("openejb.home");
+
+ if (openejbHome != null) {
+ home = new File(openejbHome);
+ }
+ return home;
+ }
+
+ public boolean stop() {
+ if (sendShutdown(5)) {
+ return true;
+ } else {
+ if (verbose) {
+ notSent();
+ }
+
+ return false;
+ }
+ }
+
+ private void notSent() {
+ System.out.println("Failed to send the shutdown notification - TomEE is likely shut down already");
+ }
+
+ public void forceStop() throws Exception {
+ if (sendShutdown(5)) {
+
+ // Check TomEE was effectively shut down after getting the message
+ // There can be concurrent shutdown operations (catalina shutdown hook for instance),
+ // so we have to wait here since it is important to be synchronous in this method
+ waitForServerShutdown();
+ } else {
+ if (verbose) {
+ notSent();
+ }
+ }
+ }
+
+ private void waitForServerShutdown() throws Exception {
+
+ if (verbose) {
+ System.out.print("Waiting for TomEE shutdown.");
+ }
+
+ final boolean b = disconnect(portShutdown, tries);
+
+ if (verbose) {
+ System.out.println();
+ }
+
+ if (!b) {
+ //We need to know about this
+ System.out.println("SEVERE: Failed to shutdown TomEE running on port " + portStartup + " using shutdown port: " + portShutdown);
+ }
+ }
+
+ /**
+ * Send the shutdown message to the running server
+ *
+ * @param attempts How many times to try to send the message before giving up
+ * @return True is the message was sent, else false if unable to connect after the defined number of attempts
+ */
+ private boolean sendShutdown(int attempts) {
+ Socket socket = null;
+ OutputStream stream = null;
+ try {
+ socket = new Socket(host, portShutdown);
+ stream = socket.getOutputStream();
+ final String shutdown = command + Character.toString((char) 0);
+ for (int i = 0; i < shutdown.length(); i++) {
+ stream.write(shutdown.charAt(i));
+ }
+ stream.flush();
+ } catch (final Exception e) {
+ if (attempts > 0) {
+ try {
+ Thread.sleep(1000);
+ } catch (final InterruptedException ie) {
+ return false;
+ }
+ return sendShutdown(--attempts);
+ } else {
+ return false;
+ }
+ } finally {
+ if (null != stream) {
+ try {
+ stream.close();
+ } catch (final Exception e) {
+ // Ignore
+ }
+ }
+ if (socket != null) {
+ try {
+ socket.close();
+ } catch (final Exception e) {
+ // Ignore
+ }
+ }
+ }
+
+ return true;
+ }
+
+ private boolean connect(final int port, int tries) {
+ if (verbose) {
+ System.out.println("[] CONNECT ATTEMPT " + (this.tries - tries) + " on port: " + port);
+ }
+
+ Socket s = null;
+ try {
+ s = new Socket();
+ s.connect(new InetSocketAddress(this.host, port), 1000);
+ s.getOutputStream().close();
+ if (verbose) {
+ System.out.println("[] CONNECTED IN " + (this.tries - tries));
+ }
+ } catch (final Exception e) {
+ if (tries < 2) {
+ if (verbose) {
+ System.out.println("[] CONNECT ATTEMPTS FAILED ( " + (this.tries - tries) + " ATTEMPTS)");
+ }
+ return false;
+ } else {
+ try {
+ Thread.sleep(1000);
+ } catch (final Exception e2) {
+ e2.printStackTrace();
+ }
+ return connect(port, --tries);
+ }
+ } finally {
+ if (s != null) {
+ try {
+ s.close();
+ } catch (final Exception ignored) {
+ // no-op
+ }
+ }
+ }
+
+ return true;
+ }
+
+ private boolean disconnect(final int port, int tries) {
+ if (verbose) {
+ System.out.println("[] DISCONNECT ATTEMPT " + (this.tries - tries) + " on port: " + port);
+ }
+
+ Socket s = null;
+ try {
+ s = new Socket();
+ s.connect(new InetSocketAddress(this.host, port), 500);
+ s.getOutputStream().close();
+
+ if (verbose) {
+ System.out.println("[] NOT DISCONNECTED AFTER ( " + (this.tries - tries) + " ATTEMPTS)");
+ }
+
+ if (tries < 2) {
+ //Give up
+ return false;
+ } else {
+ try {
+ Thread.sleep(1000);
+ } catch (final Exception e2) {
+ e2.printStackTrace();
+ }
+
+ return disconnect(port, --tries);
+ }
+
+ } catch (final IOException e) {
+ //This is what we want
+ } finally {
+ if (s != null) {
+ try {
+ s.close();
+ } catch (final Exception ignored) {
+ // no-op
+ }
+ }
+ }
+
+ return true;
+ }
+
+ public void setAdditionalClasspath(final String additionalClasspath) {
+ this.additionalClasspath = additionalClasspath;
+ }
+
+ public void killOnExit() {
+ final Process p = this.server.get();
+ if (!serverHasAlreadyBeenStarted && kill.contains(p)) {
+ return;
+ }
+
+ killOnExit(p);
+ }
+
+ private static void killOnExit(final Process p) {
+ kill.add(p);
+ }
+
+ // Shutdown hook for processes
+ private static final List<Process> kill = new ArrayList<Process>();
+
+ static {
+ Runtime.getRuntime().addShutdownHook(new CleanUpThread());
+ }
+
+ public static class CleanUpThread extends Thread {
+ @Override
+ public void run() {
+ for (final Process server : kill) {
+ try {
+ if (server != null) {
+ server.destroy();
+ server.waitFor();
+ }
+ } catch (final Throwable e) {
+ //Ignore
+ }
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/tomee/blob/6df9839b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
----------------------------------------------------------------------
diff --git a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
index 9e602b3..9936bca 100644
--- a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
+++ b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
@@ -247,7 +247,7 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
@Parameter
protected List<String> endorsedLibs;
-
+
@Parameter
protected List<String> javaagents;
@@ -740,9 +740,9 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
if (tomeeHttpsPort != null && tomeeHttpsPort > 0 && parser.value("HTTPS", null) == null) {
// ensure connector is not commented
value = value.replace("<Service name=\"Catalina\">", "<Service name=\"Catalina\">\n"
- + " <Connector port=\"" + tomeeHttpsPort + "\" protocol=\"HTTP/1.1\" SSLEnabled=\"true\"\n" +
- " scheme=\"https\" secure=\"true\"\n" +
- " clientAuth=\"false\" sslProtocol=\"TLS\" keystoreFile=\"" + keystoreFilePath + "\" />\n");
+ + " <Connector port=\"" + tomeeHttpsPort + "\" protocol=\"HTTP/1.1\" SSLEnabled=\"true\"\n" +
+ " scheme=\"https\" secure=\"true\"\n" +
+ " clientAuth=\"false\" sslProtocol=\"TLS\" keystoreFile=\"" + keystoreFilePath + "\" />\n");
}
if (tomeeHttpsPort == null) {
@@ -754,12 +754,12 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
try {
writer = new FileWriter(serverXml);
writer.write(value
- .replace(parser.http(), Integer.toString(this.getTomeeHttpPortChecked()))
- .replace(parser.https(), Integer.toString(tomeeHttpsPort))
- .replace(parser.ajp(), Integer.toString(tomeeAjpPort))
- .replace(parser.stop(), Integer.toString(this.getTomeeShutdownPortChecked()))
- .replace(parser.host(), tomeeHost)
- .replace(parser.appBase(), webappDir));
+ .replace(parser.http(), Integer.toString(this.getTomeeHttpPortChecked()))
+ .replace(parser.https(), Integer.toString(tomeeHttpsPort))
+ .replace(parser.ajp(), Integer.toString(tomeeAjpPort))
+ .replace(parser.stop(), Integer.toString(this.getTomeeShutdownPortChecked()))
+ .replace(parser.host(), tomeeHost)
+ .replace(parser.appBase(), webappDir));
} catch (final IOException e) {
throw new TomEEException(e.getMessage(), e);
} finally {
@@ -858,8 +858,8 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
server.setPortStartup(this.getTomeeHttpPortChecked());
getLog().info("Running '" + getClass().getName().replace("TomEEMojo", "").toLowerCase(Locale.ENGLISH)
- + "'. Configured TomEE in plugin is " + tomeeHost + ":" + this.getTomeeHttpPortChecked()
- + " (plugin shutdown port is " + this.getTomeeShutdownPortChecked() + " and https port is " + tomeeHttpsPort + ")");
+ + "'. Configured TomEE in plugin is " + tomeeHost + ":" + this.getTomeeHttpPortChecked()
+ + " (plugin shutdown port is " + this.getTomeeShutdownPortChecked() + " and https port is " + tomeeHttpsPort + ")");
} else {
getLog().info("Running '" + getClass().getSimpleName().replace("TomEEMojo", "").toLowerCase(Locale.ENGLISH));
}
@@ -890,7 +890,7 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
break;
}
- if("ignore".equals(line)){
+ if ("ignore".equals(line)) {
continue;
}
@@ -913,14 +913,14 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
}
private synchronized int getTomeeHttpPortChecked() {
- if(this.tomeeHttpPort <= 0){
+ if (this.tomeeHttpPort <= 0) {
this.tomeeHttpPort = NetworkUtil.getNextAvailablePort();
}
return this.tomeeHttpPort;
}
private synchronized int getTomeeShutdownPortChecked() {
- if(this.tomeeShutdownPort <= 0){
+ if (this.tomeeShutdownPort <= 0) {
this.tomeeHttpPort = NetworkUtil.getNextAvailablePort();
}
return this.tomeeHttpPort;
@@ -1176,11 +1176,19 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
}
protected void serverCmd(final RemoteServer server, final List<String> strings) {
- try {
- server.start(strings, getCmd(), checkStarted);
- } catch (final OpenEJBRuntimeException e) {
- getLog().warn("Failed to check or track server startup on port: " + this.getTomeeHttpPortChecked());
+
+ final Process p = this.server.getServer();
+
+ if (null == p) {
+
+ try {
+ server.start(strings, getCmd(), checkStarted);
+ } catch (final Exception e) {
+ //TODO - Optional server.destroy() call
+ getLog().warn("Failed to check or track server startup on port: " + this.getTomeeHttpPortChecked());
+ }
}
+
}
protected void addShutdownHooks(final RemoteServer server) {
@@ -1200,16 +1208,16 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
try {
if ("snapshots".equals(apacheRepos) || "true".equals(apacheRepos)) {
remoteRepos.add(new DefaultArtifactRepository("apache", "https://repository.apache.org/content/repositories/snapshots/",
- new DefaultRepositoryLayout(),
- new ArtifactRepositoryPolicy(true, UPDATE_POLICY_DAILY, CHECKSUM_POLICY_WARN),
- new ArtifactRepositoryPolicy(false, UPDATE_POLICY_NEVER, CHECKSUM_POLICY_WARN)));
+ new DefaultRepositoryLayout(),
+ new ArtifactRepositoryPolicy(true, UPDATE_POLICY_DAILY, CHECKSUM_POLICY_WARN),
+ new ArtifactRepositoryPolicy(false, UPDATE_POLICY_NEVER, CHECKSUM_POLICY_WARN)));
} else {
try {
new URI(apacheRepos); // to check it is a uri
remoteRepos.add(new DefaultArtifactRepository("additional-repo-tomee-mvn-plugin", apacheRepos,
- new DefaultRepositoryLayout(),
- new ArtifactRepositoryPolicy(true, UPDATE_POLICY_DAILY, CHECKSUM_POLICY_WARN),
- new ArtifactRepositoryPolicy(true, UPDATE_POLICY_NEVER, CHECKSUM_POLICY_WARN)));
+ new DefaultRepositoryLayout(),
+ new ArtifactRepositoryPolicy(true, UPDATE_POLICY_DAILY, CHECKSUM_POLICY_WARN),
+ new ArtifactRepositoryPolicy(true, UPDATE_POLICY_NEVER, CHECKSUM_POLICY_WARN)));
} catch (final URISyntaxException e) {
// ignored, use classical repos
}
@@ -1223,7 +1231,7 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
}
if ((tomeeClassifier != null && (tomeeClassifier.isEmpty() || tomeeClassifier.equals("ignore")))
- || ("org.apache.openejb".equals(tomeeGroupId) && "openejb-standalone".equals(tomeeArtifactId))) {
+ || ("org.apache.openejb".equals(tomeeGroupId) && "openejb-standalone".equals(tomeeArtifactId))) {
tomeeClassifier = null;
}
@@ -1258,8 +1266,8 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
if (!dest.exists()) {
final File parent = dest.getParentFile();
if ((!parent.exists() && !parent.mkdirs())
- || (!parent.canWrite() && !parent.setWritable(true))
- || (!parent.canRead() && !parent.setReadable(true))) {
+ || (!parent.canWrite() && !parent.setWritable(true))
+ || (!parent.canRead() && !parent.setReadable(true))) {
throw new RuntimeException("Failed to create or set permissions on: " + parent);
}
}
@@ -1316,16 +1324,16 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
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
+ || (file.exists()
+ && (
+ (apps != null && !apps.isEmpty())
+ || (!"pom".equals(packaging) && !"war".equals(packaging))))) { // webapps doesn't need apps folder in tomee
final FileWriter writer = new FileWriter(file);
final String rootTag = container.toLowerCase(Locale.ENGLISH);
writer.write("<?xml version=\"1.0\"?>\n" +
- "<" + rootTag + ">\n" +
- " <Deployments dir=\"apps\" />\n" +
- "</" + rootTag + ">\n");
+ "<" + rootTag + ">\n" +
+ " <Deployments dir=\"apps\" />\n" +
+ "</" + rootTag + ">\n");
writer.close();
final File appsFolder = new File(catalinaBase, "apps");
http://git-wip-us.apache.org/repos/asf/tomee/blob/6df9839b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java
----------------------------------------------------------------------
diff --git a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java
index 3677f98..e598d98 100644
--- a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java
+++ b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/runner/ExecRunner.java
@@ -42,7 +42,7 @@ public class ExecRunner {
public static void main(final String[] rawArgs) throws Exception {
final String[] args;
if (rawArgs == null || rawArgs.length == 0) {
- args = new String[] { "run" };
+ args = new String[]{"run"};
} else {
args = rawArgs;
}
@@ -65,8 +65,8 @@ public class ExecRunner {
final File timestampFile = new File(distribOutput, "timestamp.txt");
final boolean forceDelete = Boolean.getBoolean("tomee.runner.force-delete");
if (forceDelete
- || !timestampFile.exists()
- || Long.parseLong(IO.slurp(timestampFile).replace(System.getProperty("line.separator"), "")) < Long.parseLong(config.getProperty("timestamp"))) {
+ || !timestampFile.exists()
+ || Long.parseLong(IO.slurp(timestampFile).replace(System.getProperty("line.separator"), "")) < Long.parseLong(config.getProperty("timestamp"))) {
if (forceDelete || timestampFile.exists()) {
System.out.println("Deleting " + distribOutput.getAbsolutePath());
Files.delete(distribOutput);
@@ -80,7 +80,7 @@ public class ExecRunner {
if (scripts != null) { // dont use filefilter to avoid dependency issue
for (final File f : scripts) {
if (f.getName().endsWith(".sh") && !f.canExecute()) {
- if(!f.setExecutable(true, true)){
+ if (!f.setExecutable(true, true)) {
System.err.println("Failed make file executable: " + f);
}
}
@@ -94,14 +94,14 @@ public class ExecRunner {
final String dir = cmd.substring(0, lastSlash);
final boolean isWin = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("win");
final String script = cmd.substring(lastSlash + 1, cmd.length() - SH_BAT_AUTO.length()).replace('/', isWin ? '\\' : '/')
- + (isWin ? ".bat" : ".sh");
+ + (isWin ? ".bat" : ".sh");
final File scriptFile = new File(distribOutput, dir + File.separator + script);
if (!scriptFile.exists()) {
throw new IllegalArgumentException("Can't find " + cmd);
}
cmd = scriptFile.getAbsolutePath();
if (cmd.endsWith(".sh")) {
- if(!scriptFile.setExecutable(true)){
+ if (!scriptFile.setExecutable(true)) {
System.err.println("Failed make script file executable: " + scriptFile);
}
}
@@ -130,7 +130,7 @@ public class ExecRunner {
}
}
- final QuickServerXmlParser parser = QuickServerXmlParser.parse(new File(distribOutput,"conf/server.xml"));
+ final QuickServerXmlParser parser = QuickServerXmlParser.parse(new File(distribOutput, "conf/server.xml"));
System.setProperty("openejb.home", distribOutput.getAbsolutePath());
System.setProperty("server.shutdown.port", parser.stop());
@@ -156,8 +156,12 @@ public class ExecRunner {
if ("run".equals(args[0])) {
args[0] = "start";
}
- server.start(jvmArgs, args[0], true);
- server.getServer().waitFor();
+ try {
+ server.start(jvmArgs, args[0], true);
+ server.getServer().waitFor();
+ } catch (final Exception e) {
+ server.destroy();
+ }
} else {
// TODO: split cmd correctly to support multiple inlined segments in cmd
if (cmd.endsWith(".bat") && !cmd.startsWith("cmd.exe")) {
@@ -178,7 +182,7 @@ public class ExecRunner {
boolean redirectOut = false;
try { // java >= 7
ProcessBuilder.class.getDeclaredMethod("inheritIO").invoke(builder);
- } catch (final Throwable th){ // java 6
+ } catch (final Throwable th) { // java 6
redirectOut = true;
}
http://git-wip-us.apache.org/repos/asf/tomee/blob/6df9839b/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/ContainersImplTomEE.java
----------------------------------------------------------------------
diff --git a/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/ContainersImplTomEE.java b/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/ContainersImplTomEE.java
index 301a8d1..e4a71f5 100644
--- a/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/ContainersImplTomEE.java
+++ b/tck/tck-common/src/main/java/org/apache/openejb/tck/impl/ContainersImplTomEE.java
@@ -1,164 +1,169 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.openejb.tck.impl;
-
-import org.apache.openejb.assembler.Deployer;
-import org.apache.openejb.assembler.classic.AppInfo;
-import org.apache.openejb.client.RemoteInitialContextFactory;
-import org.apache.openejb.config.RemoteServer;
-import org.apache.openejb.config.ValidationException;
-import org.apache.openejb.loader.Options;
-import org.apache.openejb.tck.OpenEJBTCKRuntimeException;
-import org.apache.openejb.tck.util.ServerLocal;
-import org.jboss.testharness.api.DeploymentException;
-import org.jboss.testharness.spi.Containers;
-
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Arrays;
-import java.util.Properties;
-
-/**
- * @version $Rev$ $Date$
- */
-public class ContainersImplTomEE extends AbstractContainers implements Containers {
- private static int count = 0;
- private final RemoteServer server;
- private Deployer deployer = null;
- private Exception exception;
- private AppInfo appInfo;
- private File currentFile = null;
- private final int port = ServerLocal.getPort(8080);
-
- private Deployer lookup() {
- final Options options = new Options(System.getProperties());
- final Properties props = new Properties();
- props.put(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
- props.put(Context.PROVIDER_URL, options.get(Context.PROVIDER_URL, "http://localhost:" + port + "/tomee/ejb"));
-
- final String deployerJndi = System.getProperty("openejb.deployer.jndiname", "openejb/DeployerBusinessRemote");
-
- try {
- final InitialContext context = new InitialContext(props);
- return (Deployer) context.lookup(deployerJndi);
- } catch (final Exception e) {
- throw new OpenEJBTCKRuntimeException(e);
- }
- }
-
- public ContainersImplTomEE() {
- System.out.println("ContainersImpl=" + ContainersImplTomEE.class.getName());
- System.out.println("Initialized ContainersImplTomEE " + (++count));
- server = new RemoteServer();
- server.setPortStartup(this.port);
- }
-
- @Override
- public boolean deploy(final InputStream archive, final String name) throws IOException {
- exception = null;
- appInfo = null;
-
- System.out.println("Deploying " + archive + " with name " + name);
-
- currentFile = getFile(name);
- System.out.println(currentFile);
- writeToFile(currentFile, archive);
- try {
- if (deployer == null) {
- deployer = lookup();
- }
- appInfo = deployer.deploy(currentFile.getAbsolutePath());
- } catch (final Exception ex) {
- Exception e = ex;
- if (e.getCause() instanceof ValidationException) {
- e = (Exception) e.getCause();
- }
-
- if (name.contains(".broken.")) {
- // Tests that contain the name '.broken.' are expected to fail deployment
- // This is how the TCK verifies the container is doing the required error checking
- exception = (DeploymentException) new DeploymentException("deploy failed").initCause(e);
- } else {
- // This on the other hand is not good ....
- System.out.println("FIX Deployment of " + name);
- e.printStackTrace();
- exception = e;
- }
-
- return false;
- }
- return true;
- }
-
- @Override
- public DeploymentException getDeploymentException() {
- try {
- return (DeploymentException) exception;
- } catch (final Exception e) {
- System.out.println("BADCAST");
- return new DeploymentException("", exception);
- }
- }
-
- @Override
- public void undeploy(final String name) throws IOException {
- if (appInfo == null) {
- if (!(exception instanceof DeploymentException)) {
- System.out.println("Nothing to undeploy" + name);
- }
- return;
- }
-
- System.out.println("Undeploying " + name);
- try {
- deployer.undeploy(appInfo.path);
- } catch (final Exception e) {
- e.printStackTrace();
- throw new OpenEJBTCKRuntimeException(e);
- }
-
- final File toDelete;
- if (currentFile != null && (toDelete = currentFile.getParentFile()).exists()) {
- System.out.println("deleting " + toDelete.getAbsolutePath());
- delete(toDelete);
- }
- }
-
- protected File getFile(final String name) {
- final File dir = new File(tmpDir, Math.random() + "");
- if (!dir.exists() && !dir.mkdir()) {
- throw new RuntimeException("Failed to create directory: " + dir);
- }
- dir.deleteOnExit();
- return new File(dir, name);
- }
-
- @Override
- public void setup() throws IOException {
- System.out.println("Setup called");
- server.start(Arrays.asList("-Dopenejb.classloader.forced-load=org.apache.openejb.tck"), "start", true);
- }
-
- @Override
- public void cleanup() throws IOException {
- System.out.println("Cleanup called");
- server.destroy();
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.tck.impl;
+
+import org.apache.openejb.assembler.Deployer;
+import org.apache.openejb.assembler.classic.AppInfo;
+import org.apache.openejb.client.RemoteInitialContextFactory;
+import org.apache.openejb.config.RemoteServer;
+import org.apache.openejb.config.ValidationException;
+import org.apache.openejb.loader.Options;
+import org.apache.openejb.tck.OpenEJBTCKRuntimeException;
+import org.apache.openejb.tck.util.ServerLocal;
+import org.jboss.testharness.api.DeploymentException;
+import org.jboss.testharness.spi.Containers;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.Properties;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ContainersImplTomEE extends AbstractContainers implements Containers {
+ private static int count = 0;
+ private final RemoteServer server;
+ private Deployer deployer = null;
+ private Exception exception;
+ private AppInfo appInfo;
+ private File currentFile = null;
+ private final int port = ServerLocal.getPort(8080);
+
+ private Deployer lookup() {
+ final Options options = new Options(System.getProperties());
+ final Properties props = new Properties();
+ props.put(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
+ props.put(Context.PROVIDER_URL, options.get(Context.PROVIDER_URL, "http://localhost:" + port + "/tomee/ejb"));
+
+ final String deployerJndi = System.getProperty("openejb.deployer.jndiname", "openejb/DeployerBusinessRemote");
+
+ try {
+ final InitialContext context = new InitialContext(props);
+ return (Deployer) context.lookup(deployerJndi);
+ } catch (final Exception e) {
+ throw new OpenEJBTCKRuntimeException(e);
+ }
+ }
+
+ public ContainersImplTomEE() {
+ System.out.println("ContainersImpl=" + ContainersImplTomEE.class.getName());
+ System.out.println("Initialized ContainersImplTomEE " + (++count));
+ server = new RemoteServer();
+ server.setPortStartup(this.port);
+ }
+
+ @Override
+ public boolean deploy(final InputStream archive, final String name) throws IOException {
+ exception = null;
+ appInfo = null;
+
+ System.out.println("Deploying " + archive + " with name " + name);
+
+ currentFile = getFile(name);
+ System.out.println(currentFile);
+ writeToFile(currentFile, archive);
+ try {
+ if (deployer == null) {
+ deployer = lookup();
+ }
+ appInfo = deployer.deploy(currentFile.getAbsolutePath());
+ } catch (final Exception ex) {
+ Exception e = ex;
+ if (e.getCause() instanceof ValidationException) {
+ e = (Exception) e.getCause();
+ }
+
+ if (name.contains(".broken.")) {
+ // Tests that contain the name '.broken.' are expected to fail deployment
+ // This is how the TCK verifies the container is doing the required error checking
+ exception = (DeploymentException) new DeploymentException("deploy failed").initCause(e);
+ } else {
+ // This on the other hand is not good ....
+ System.out.println("FIX Deployment of " + name);
+ e.printStackTrace();
+ exception = e;
+ }
+
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public DeploymentException getDeploymentException() {
+ try {
+ return (DeploymentException) exception;
+ } catch (final Exception e) {
+ System.out.println("BADCAST");
+ return new DeploymentException("", exception);
+ }
+ }
+
+ @Override
+ public void undeploy(final String name) throws IOException {
+ if (appInfo == null) {
+ if (!(exception instanceof DeploymentException)) {
+ System.out.println("Nothing to undeploy" + name);
+ }
+ return;
+ }
+
+ System.out.println("Undeploying " + name);
+ try {
+ deployer.undeploy(appInfo.path);
+ } catch (final Exception e) {
+ e.printStackTrace();
+ throw new OpenEJBTCKRuntimeException(e);
+ }
+
+ final File toDelete;
+ if (currentFile != null && (toDelete = currentFile.getParentFile()).exists()) {
+ System.out.println("deleting " + toDelete.getAbsolutePath());
+ delete(toDelete);
+ }
+ }
+
+ protected File getFile(final String name) {
+ final File dir = new File(tmpDir, Math.random() + "");
+ if (!dir.exists() && !dir.mkdir()) {
+ throw new RuntimeException("Failed to create directory: " + dir);
+ }
+ dir.deleteOnExit();
+ return new File(dir, name);
+ }
+
+ @Override
+ public void setup() throws IOException {
+ System.out.println("Setup called");
+ try {
+ server.start(Arrays.asList("-Dopenejb.classloader.forced-load=org.apache.openejb.tck"), "start", true);
+ } catch (final Exception e) {
+ cleanup();
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void cleanup() throws IOException {
+ System.out.println("Cleanup called");
+ server.destroy();
+ }
+}
[3/3] tomee git commit: Delegate RemoteServer.destroy() to usage -
Try/catch
Posted by an...@apache.org.
Delegate RemoteServer.destroy() to usage - Try/catch
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/6df9839b
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/6df9839b
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/6df9839b
Branch: refs/heads/tomee-1.7.x
Commit: 6df9839b2804e4e3870e9797f1c01f01264a466f
Parents: e113c06
Author: AndyGee <an...@gmx.de>
Authored: Sat Nov 7 02:26:52 2015 +0100
Committer: AndyGee <an...@gmx.de>
Committed: Sat Nov 7 02:26:52 2015 +0100
----------------------------------------------------------------------
.../arquillian/remote/RemoteTomEEContainer.java | 713 ++++-----
.../arquillian/webapp/TomEEWebappContainer.java | 489 +++---
.../org/apache/openejb/config/RemoteServer.java | 1418 +++++++++---------
.../openejb/maven/plugin/AbstractTomEEMojo.java | 78 +-
.../openejb/maven/plugin/runner/ExecRunner.java | 24 +-
.../openejb/tck/impl/ContainersImplTomEE.java | 333 ++--
.../openejb/tck/impl/FullRestartContainer.java | 273 ++--
.../apache/tomee/RemoteTomEEEJBContainer.java | 315 ++--
8 files changed, 1843 insertions(+), 1800 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tomee/blob/6df9839b/arquillian/arquillian-tomee-remote/src/main/java/org/apache/tomee/arquillian/remote/RemoteTomEEContainer.java
----------------------------------------------------------------------
diff --git a/arquillian/arquillian-tomee-remote/src/main/java/org/apache/tomee/arquillian/remote/RemoteTomEEContainer.java b/arquillian/arquillian-tomee-remote/src/main/java/org/apache/tomee/arquillian/remote/RemoteTomEEContainer.java
index 15fb446..80e3173 100644
--- a/arquillian/arquillian-tomee-remote/src/main/java/org/apache/tomee/arquillian/remote/RemoteTomEEContainer.java
+++ b/arquillian/arquillian-tomee-remote/src/main/java/org/apache/tomee/arquillian/remote/RemoteTomEEContainer.java
@@ -1,356 +1,357 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.tomee.arquillian.remote;
-
-import org.apache.openejb.arquillian.common.ArquillianFilterRunner;
-import org.apache.openejb.arquillian.common.ArquillianUtil;
-import org.apache.openejb.arquillian.common.Files;
-import org.apache.openejb.arquillian.common.IO;
-import org.apache.openejb.arquillian.common.Setup;
-import org.apache.openejb.arquillian.common.TomEEContainer;
-import org.apache.openejb.assembler.Deployer;
-import org.apache.openejb.assembler.DeployerEjb;
-import org.apache.openejb.config.RemoteServer;
-import org.apache.openejb.util.NetworkUtil;
-import org.jboss.arquillian.container.spi.client.container.LifecycleException;
-import org.jboss.arquillian.protocol.servlet.ServletMethodExecutor;
-import org.jboss.shrinkwrap.api.Archive;
-
-import javax.naming.NamingException;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.TreeMap;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-public class RemoteTomEEContainer extends TomEEContainer<RemoteTomEEConfiguration> {
- private static final Logger logger = Logger.getLogger(RemoteTomEEContainer.class.getName());
-
- private static final String ARQUILLIAN_FILTER = "-Dorg.apache.openejb.servlet.filters=" + ArquillianFilterRunner.class.getName() + "=" + ServletMethodExecutor.ARQUILLIAN_SERVLET_MAPPING;
-
- private RemoteServer container;
- private boolean shutdown;
- private File tomeeHome;
- private Collection<Archive<?>> containerArchives;
- private final Properties deployerProperties = new Properties();
-
- @Override
- public void setup(final RemoteTomEEConfiguration configuration) {
- super.setup(configuration);
-
- if (configuration.getDeployerProperties() != null) {
- try {
- final InputStream bytes = IO.read(configuration.getDeployerProperties().getBytes());
- IO.readProperties(bytes, deployerProperties);
- } catch (final IOException e) {
- logger.log(Level.SEVERE, "Can't parse <property name=\"properties\"> value '" + configuration.getProperties() + "'", e);
- }
- }
- }
-
- @Override
- public void start() throws LifecycleException {
- // see if TomEE is already running by checking the http port
- final int httpPort = configuration.getHttpPort();
-
- if (Setup.isRunning(configuration.getHost(), httpPort)) {
-
- String host = "local";
-
- if (!NetworkUtil.isLocalAddress(configuration.getHost())) {
- //Supply at least this property so that the archive is transmitted on deploy
- if (null == deployerProperties.getProperty(DeployerEjb.OPENEJB_USE_BINARIES)) {
- deployerProperties.setProperty(DeployerEjb.OPENEJB_USE_BINARIES, "true");
- }
- host = "remote";
- }
-
- logger.info(String.format("TomEE found running on %s port %s", host, httpPort));
-
- return;
- }
-
- shutdown = true;
-
- final String shutdownPort = System.getProperty(RemoteServer.SERVER_SHUTDOWN_PORT);
- final String shutdownHost = System.getProperty(RemoteServer.SERVER_SHUTDOWN_HOST);
- final String shutdownCommand = System.getProperty(RemoteServer.SERVER_SHUTDOWN_COMMAND);
- final String debug = System.getProperty(RemoteServer.OPENEJB_SERVER_DEBUG);
- final String debugPort = System.getProperty(RemoteServer.SERVER_DEBUG_PORT);
-
- try {
-
- configure();
-
- final int stopPort = configuration.getStopPort();
- System.setProperty(RemoteServer.SERVER_SHUTDOWN_PORT, Integer.toString(stopPort));
- System.setProperty(RemoteServer.SERVER_SHUTDOWN_COMMAND, configuration.getStopCommand());
- System.setProperty(RemoteServer.SERVER_SHUTDOWN_HOST, configuration.getStopHost());
-
- if (configuration.isDebug()) {
- System.setProperty(RemoteServer.OPENEJB_SERVER_DEBUG, "true");
- System.setProperty(RemoteServer.SERVER_DEBUG_PORT, Integer.toString(configuration.getDebugPort()));
- }
-
- container = new RemoteServer();
- container.setPortStartup(httpPort);
- container.start(args(), "start", true);
- container.killOnExit();
-
- if (configuration.getProperties() != null) {
- final Properties props = new Properties();
- IO.readProperties(IO.read(configuration.getProperties().getBytes()), props);
-
- containerArchives = ArquillianUtil.toDeploy(props);
- for (final Archive<?> archive : containerArchives) {
- deploy(archive);
- }
- }
- } catch (final Exception e) {
- logger.log(Level.SEVERE, "Unable to start remote container", e);
- throw new LifecycleException("Unable to start remote container:" + e.getMessage(), e);
- } finally {
- resetSystemProperty(RemoteServer.SERVER_SHUTDOWN_PORT, shutdownPort);
- resetSystemProperty(RemoteServer.SERVER_SHUTDOWN_HOST, shutdownHost);
- resetSystemProperty(RemoteServer.SERVER_SHUTDOWN_COMMAND, shutdownCommand);
- resetSystemProperty(RemoteServer.OPENEJB_SERVER_DEBUG, debug);
- resetSystemProperty(RemoteServer.SERVER_DEBUG_PORT, debugPort);
- }
- }
-
- @Override
- protected Properties getDeployerProperties() {
- if (deployerProperties.isEmpty()) {
- return null;
- }
- return deployerProperties;
- }
-
- private List<String> args() {
- String opts = configuration.getCatalina_opts();
- if (opts != null) {
- opts = opts.trim();
- }
- if (opts == null || opts.isEmpty()) {
- return Arrays.asList("-Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=false", ARQUILLIAN_FILTER);
- }
-
- final List<String> splitOnSpace = new ArrayList<String>();
-
- final Iterator<String> it = new ArgsIterator(opts);
- while (it.hasNext()) {
- splitOnSpace.add(it.next());
- }
-
- if (!splitOnSpace.contains("-Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=true")) {
- splitOnSpace.add("-Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=false");
- }
- splitOnSpace.add(ARQUILLIAN_FILTER);
- return splitOnSpace;
- }
-
- private static void resetSystemProperty(final String key, final String value) {
- if (value == null) {
- System.getProperties().remove(key);
- } else {
- System.setProperty(key, value);
- }
- }
-
- private void configure() throws LifecycleException, IOException {
- final File workingDirectory = new File(configuration.getDir()).getAbsoluteFile();
-
- if (configuration.getCleanOnStartUp()) {
- Files.delete(workingDirectory);
- }
-
- if (workingDirectory.exists()) {
-
- Files.assertDir(workingDirectory);
-
- } else {
-
- Files.mkdir(workingDirectory);
- Files.deleteOnExit(workingDirectory);
- }
-
- Files.readable(workingDirectory);
- Files.writable(workingDirectory);
-
- tomeeHome = Setup.findHome(workingDirectory);
-
- if (tomeeHome == null) {
- tomeeHome = Setup.downloadAndUnpack(workingDirectory, configuration.getArtifactName());
-
- logger.log(Level.INFO, "Downloaded container to: " + tomeeHome);
- }
-
- Files.assertDir(tomeeHome);
- Files.readable(tomeeHome);
- Files.writable(tomeeHome);
-
- Setup.synchronizeFolder(tomeeHome, configuration.getConf(), "conf");
- Setup.synchronizeFolder(tomeeHome, configuration.getBin(), "bin");
- Setup.synchronizeFolder(tomeeHome, configuration.getLib(), "lib");
- Setup.addTomEELibraries(tomeeHome, configuration.getAdditionalLibs());
-
- Setup.configureServerXml(tomeeHome, configuration);
-
- Setup.configureSystemProperties(tomeeHome, configuration);
-
- final String opts = configuration.getCatalina_opts();
-
- Setup.exportProperties(tomeeHome, configuration, opts == null || (!opts.contains("-Xm") && !opts.matches(".*-XX:[^=]*Size=.*")));
- Setup.installArquillianBeanDiscoverer(tomeeHome);
-
- if (configuration.isRemoveUnusedWebapps()) {
- Setup.removeUselessWebapps(tomeeHome);
- }
-
- if (configuration.isSimpleLog() && noLoggingConfigProvided()) {
- final File loggingProperties = Files.path(tomeeHome, "conf", "logging.properties");
-
- final Properties logging = new Properties();
- logging.put("handlers", "java.util.logging.ConsoleHandler");
- logging.put(".handlers", "java.util.logging.ConsoleHandler");
- logging.put("java.util.logging.ConsoleHandler.level", "INFO");
- logging.put("java.util.logging.ConsoleHandler.formatter", "org.apache.tomee.jul.formatter.SimpleTomEEFormatter");
-
- IO.writeProperties(loggingProperties, logging);
- }
-
- if (logger.isLoggable(Level.FINE)) {
- final Map<Object, Object> map = new TreeMap<Object, Object>(System.getProperties());
- for (final Map.Entry<Object, Object> entry : map.entrySet()) {
- logger.log(Level.FINE, String.format("%s = %s\n", entry.getKey(), entry.getValue()));
- }
- }
- }
-
- private boolean noLoggingConfigProvided() {
- if (configuration.getConf() == null) {
- return true;
- }
-
- final File conf = new File(configuration.getConf());
-
- return !(conf.exists()
- && (new File(conf, "logging.properties").exists()
- || new File(conf, "log4j.properties").exists()
- || new File(conf, "log4j.xml").exists()));
- }
-
- @Override
- public void stop() throws LifecycleException {
- ArquillianUtil.undeploy(this, containerArchives);
-
- // only stop the container if we started it
- if (shutdown) {
- Setup.removeArquillianBeanDiscoverer(tomeeHome);
- container.destroy();
- }
- }
-
- @Override
- public Class<RemoteTomEEConfiguration> getConfigurationClass() {
- return RemoteTomEEConfiguration.class;
- }
-
- @Override
- protected Deployer deployer() throws NamingException {
- try {
- return super.deployer();
- } catch (final RuntimeException ne) {
- // some debug lines
- if (Boolean.getBoolean("openejb.arquillian.debug")) {
- container.kill3UNIX();
- LOGGER.info("Can't connect to deployer through: " + providerUrl());
- try {
- LOGGER.info("Here is the server.xml:\n" + IO.slurp(new File(Setup.findHome(new File(configuration.getDir()).getAbsoluteFile()), "conf/server.xml")));
- } catch (final IOException ignored) {
- // no-op
- }
- }
- throw ne;
- }
- }
-
- private static class ArgsIterator implements Iterator<String> {
- private final String string;
- private int currentIndex;
-
- public ArgsIterator(final String opts) {
- string = opts;
- currentIndex = 0;
- }
-
- @Override
- public boolean hasNext() {
- return string != null && currentIndex < string.length();
- }
-
- @Override
- public String next() {
- skipWhiteCharacters();
-
- if (done()) {
- throw new UnsupportedOperationException("No more element");
- }
-
- final char endChar;
- if (string.charAt(currentIndex) == '"') {
- currentIndex++;
- endChar = '"';
- } else {
- endChar = ' ';
- }
-
- final int start = currentIndex;
- int end = string.indexOf(endChar, currentIndex + 1);
- if (end <= 0) {
- end = string.length();
- }
-
- currentIndex = end + 1;
-
- return string.substring(start, end);
- }
-
- private void skipWhiteCharacters() {
- while (!done() && (string.charAt(currentIndex) == ' ' || string.charAt(currentIndex) == '\t')) {
- currentIndex++;
- }
- }
-
- private boolean done() {
- return currentIndex >= string.length();
- }
-
- @Override
- public void remove() {
- throw new UnsupportedOperationException();
- }
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomee.arquillian.remote;
+
+import org.apache.openejb.arquillian.common.ArquillianFilterRunner;
+import org.apache.openejb.arquillian.common.ArquillianUtil;
+import org.apache.openejb.arquillian.common.Files;
+import org.apache.openejb.arquillian.common.IO;
+import org.apache.openejb.arquillian.common.Setup;
+import org.apache.openejb.arquillian.common.TomEEContainer;
+import org.apache.openejb.assembler.Deployer;
+import org.apache.openejb.assembler.DeployerEjb;
+import org.apache.openejb.config.RemoteServer;
+import org.apache.openejb.util.NetworkUtil;
+import org.jboss.arquillian.container.spi.client.container.LifecycleException;
+import org.jboss.arquillian.protocol.servlet.ServletMethodExecutor;
+import org.jboss.shrinkwrap.api.Archive;
+
+import javax.naming.NamingException;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.TreeMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class RemoteTomEEContainer extends TomEEContainer<RemoteTomEEConfiguration> {
+ private static final Logger logger = Logger.getLogger(RemoteTomEEContainer.class.getName());
+
+ private static final String ARQUILLIAN_FILTER = "-Dorg.apache.openejb.servlet.filters=" + ArquillianFilterRunner.class.getName() + "=" + ServletMethodExecutor.ARQUILLIAN_SERVLET_MAPPING;
+
+ private RemoteServer container;
+ private boolean shutdown;
+ private File tomeeHome;
+ private Collection<Archive<?>> containerArchives;
+ private final Properties deployerProperties = new Properties();
+
+ @Override
+ public void setup(final RemoteTomEEConfiguration configuration) {
+ super.setup(configuration);
+
+ if (configuration.getDeployerProperties() != null) {
+ try {
+ final InputStream bytes = IO.read(configuration.getDeployerProperties().getBytes());
+ IO.readProperties(bytes, deployerProperties);
+ } catch (final IOException e) {
+ logger.log(Level.SEVERE, "Can't parse <property name=\"properties\"> value '" + configuration.getProperties() + "'", e);
+ }
+ }
+ }
+
+ @Override
+ public void start() throws LifecycleException {
+ // see if TomEE is already running by checking the http port
+ final int httpPort = configuration.getHttpPort();
+
+ if (Setup.isRunning(configuration.getHost(), httpPort)) {
+
+ String host = "local";
+
+ if (!NetworkUtil.isLocalAddress(configuration.getHost())) {
+ //Supply at least this property so that the archive is transmitted on deploy
+ if (null == deployerProperties.getProperty(DeployerEjb.OPENEJB_USE_BINARIES)) {
+ deployerProperties.setProperty(DeployerEjb.OPENEJB_USE_BINARIES, "true");
+ }
+ host = "remote";
+ }
+
+ logger.info(String.format("TomEE found running on %s port %s", host, httpPort));
+
+ return;
+ }
+
+ shutdown = true;
+
+ final String shutdownPort = System.getProperty(RemoteServer.SERVER_SHUTDOWN_PORT);
+ final String shutdownHost = System.getProperty(RemoteServer.SERVER_SHUTDOWN_HOST);
+ final String shutdownCommand = System.getProperty(RemoteServer.SERVER_SHUTDOWN_COMMAND);
+ final String debug = System.getProperty(RemoteServer.OPENEJB_SERVER_DEBUG);
+ final String debugPort = System.getProperty(RemoteServer.SERVER_DEBUG_PORT);
+
+ try {
+
+ configure();
+
+ final int stopPort = configuration.getStopPort();
+ System.setProperty(RemoteServer.SERVER_SHUTDOWN_PORT, Integer.toString(stopPort));
+ System.setProperty(RemoteServer.SERVER_SHUTDOWN_COMMAND, configuration.getStopCommand());
+ System.setProperty(RemoteServer.SERVER_SHUTDOWN_HOST, configuration.getStopHost());
+
+ if (configuration.isDebug()) {
+ System.setProperty(RemoteServer.OPENEJB_SERVER_DEBUG, "true");
+ System.setProperty(RemoteServer.SERVER_DEBUG_PORT, Integer.toString(configuration.getDebugPort()));
+ }
+
+ container = new RemoteServer();
+ container.setPortStartup(httpPort);
+ container.start(args(), "start", true);
+ container.killOnExit();
+
+ if (configuration.getProperties() != null) {
+ final Properties props = new Properties();
+ IO.readProperties(IO.read(configuration.getProperties().getBytes()), props);
+
+ containerArchives = ArquillianUtil.toDeploy(props);
+ for (final Archive<?> archive : containerArchives) {
+ deploy(archive);
+ }
+ }
+ } catch (final Exception e) {
+ container.destroy();
+ logger.log(Level.SEVERE, "Unable to start remote container", e);
+ throw new LifecycleException("Unable to start remote container:" + e.getMessage(), e);
+ } finally {
+ resetSystemProperty(RemoteServer.SERVER_SHUTDOWN_PORT, shutdownPort);
+ resetSystemProperty(RemoteServer.SERVER_SHUTDOWN_HOST, shutdownHost);
+ resetSystemProperty(RemoteServer.SERVER_SHUTDOWN_COMMAND, shutdownCommand);
+ resetSystemProperty(RemoteServer.OPENEJB_SERVER_DEBUG, debug);
+ resetSystemProperty(RemoteServer.SERVER_DEBUG_PORT, debugPort);
+ }
+ }
+
+ @Override
+ protected Properties getDeployerProperties() {
+ if (deployerProperties.isEmpty()) {
+ return null;
+ }
+ return deployerProperties;
+ }
+
+ private List<String> args() {
+ String opts = configuration.getCatalina_opts();
+ if (opts != null) {
+ opts = opts.trim();
+ }
+ if (opts == null || opts.isEmpty()) {
+ return Arrays.asList("-Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=false", ARQUILLIAN_FILTER);
+ }
+
+ final List<String> splitOnSpace = new ArrayList<String>();
+
+ final Iterator<String> it = new ArgsIterator(opts);
+ while (it.hasNext()) {
+ splitOnSpace.add(it.next());
+ }
+
+ if (!splitOnSpace.contains("-Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=true")) {
+ splitOnSpace.add("-Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=false");
+ }
+ splitOnSpace.add(ARQUILLIAN_FILTER);
+ return splitOnSpace;
+ }
+
+ private static void resetSystemProperty(final String key, final String value) {
+ if (value == null) {
+ System.getProperties().remove(key);
+ } else {
+ System.setProperty(key, value);
+ }
+ }
+
+ private void configure() throws LifecycleException, IOException {
+ final File workingDirectory = new File(configuration.getDir()).getAbsoluteFile();
+
+ if (configuration.getCleanOnStartUp()) {
+ Files.delete(workingDirectory);
+ }
+
+ if (workingDirectory.exists()) {
+
+ Files.assertDir(workingDirectory);
+
+ } else {
+
+ Files.mkdir(workingDirectory);
+ Files.deleteOnExit(workingDirectory);
+ }
+
+ Files.readable(workingDirectory);
+ Files.writable(workingDirectory);
+
+ tomeeHome = Setup.findHome(workingDirectory);
+
+ if (tomeeHome == null) {
+ tomeeHome = Setup.downloadAndUnpack(workingDirectory, configuration.getArtifactName());
+
+ logger.log(Level.INFO, "Downloaded container to: " + tomeeHome);
+ }
+
+ Files.assertDir(tomeeHome);
+ Files.readable(tomeeHome);
+ Files.writable(tomeeHome);
+
+ Setup.synchronizeFolder(tomeeHome, configuration.getConf(), "conf");
+ Setup.synchronizeFolder(tomeeHome, configuration.getBin(), "bin");
+ Setup.synchronizeFolder(tomeeHome, configuration.getLib(), "lib");
+ Setup.addTomEELibraries(tomeeHome, configuration.getAdditionalLibs());
+
+ Setup.configureServerXml(tomeeHome, configuration);
+
+ Setup.configureSystemProperties(tomeeHome, configuration);
+
+ final String opts = configuration.getCatalina_opts();
+
+ Setup.exportProperties(tomeeHome, configuration, opts == null || (!opts.contains("-Xm") && !opts.matches(".*-XX:[^=]*Size=.*")));
+ Setup.installArquillianBeanDiscoverer(tomeeHome);
+
+ if (configuration.isRemoveUnusedWebapps()) {
+ Setup.removeUselessWebapps(tomeeHome);
+ }
+
+ if (configuration.isSimpleLog() && noLoggingConfigProvided()) {
+ final File loggingProperties = Files.path(tomeeHome, "conf", "logging.properties");
+
+ final Properties logging = new Properties();
+ logging.put("handlers", "java.util.logging.ConsoleHandler");
+ logging.put(".handlers", "java.util.logging.ConsoleHandler");
+ logging.put("java.util.logging.ConsoleHandler.level", "INFO");
+ logging.put("java.util.logging.ConsoleHandler.formatter", "org.apache.tomee.jul.formatter.SimpleTomEEFormatter");
+
+ IO.writeProperties(loggingProperties, logging);
+ }
+
+ if (logger.isLoggable(Level.FINE)) {
+ final Map<Object, Object> map = new TreeMap<Object, Object>(System.getProperties());
+ for (final Map.Entry<Object, Object> entry : map.entrySet()) {
+ logger.log(Level.FINE, String.format("%s = %s\n", entry.getKey(), entry.getValue()));
+ }
+ }
+ }
+
+ private boolean noLoggingConfigProvided() {
+ if (configuration.getConf() == null) {
+ return true;
+ }
+
+ final File conf = new File(configuration.getConf());
+
+ return !(conf.exists()
+ && (new File(conf, "logging.properties").exists()
+ || new File(conf, "log4j.properties").exists()
+ || new File(conf, "log4j.xml").exists()));
+ }
+
+ @Override
+ public void stop() throws LifecycleException {
+ ArquillianUtil.undeploy(this, containerArchives);
+
+ // only stop the container if we started it
+ if (shutdown) {
+ Setup.removeArquillianBeanDiscoverer(tomeeHome);
+ container.destroy();
+ }
+ }
+
+ @Override
+ public Class<RemoteTomEEConfiguration> getConfigurationClass() {
+ return RemoteTomEEConfiguration.class;
+ }
+
+ @Override
+ protected Deployer deployer() throws NamingException {
+ try {
+ return super.deployer();
+ } catch (final RuntimeException ne) {
+ // some debug lines
+ if (Boolean.getBoolean("openejb.arquillian.debug")) {
+ container.kill3UNIX();
+ LOGGER.info("Can't connect to deployer through: " + providerUrl());
+ try {
+ LOGGER.info("Here is the server.xml:\n" + IO.slurp(new File(Setup.findHome(new File(configuration.getDir()).getAbsoluteFile()), "conf/server.xml")));
+ } catch (final IOException ignored) {
+ // no-op
+ }
+ }
+ throw ne;
+ }
+ }
+
+ private static class ArgsIterator implements Iterator<String> {
+ private final String string;
+ private int currentIndex;
+
+ public ArgsIterator(final String opts) {
+ string = opts;
+ currentIndex = 0;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return string != null && currentIndex < string.length();
+ }
+
+ @Override
+ public String next() {
+ skipWhiteCharacters();
+
+ if (done()) {
+ throw new UnsupportedOperationException("No more element");
+ }
+
+ final char endChar;
+ if (string.charAt(currentIndex) == '"') {
+ currentIndex++;
+ endChar = '"';
+ } else {
+ endChar = ' ';
+ }
+
+ final int start = currentIndex;
+ int end = string.indexOf(endChar, currentIndex + 1);
+ if (end <= 0) {
+ end = string.length();
+ }
+
+ currentIndex = end + 1;
+
+ return string.substring(start, end);
+ }
+
+ private void skipWhiteCharacters() {
+ while (!done() && (string.charAt(currentIndex) == ' ' || string.charAt(currentIndex) == '\t')) {
+ currentIndex++;
+ }
+ }
+
+ private boolean done() {
+ return currentIndex >= string.length();
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/tomee/blob/6df9839b/arquillian/arquillian-tomee-webapp-remote/src/main/java/org/apache/tomee/arquillian/webapp/TomEEWebappContainer.java
----------------------------------------------------------------------
diff --git a/arquillian/arquillian-tomee-webapp-remote/src/main/java/org/apache/tomee/arquillian/webapp/TomEEWebappContainer.java b/arquillian/arquillian-tomee-webapp-remote/src/main/java/org/apache/tomee/arquillian/webapp/TomEEWebappContainer.java
index ddd0301..3e07e81 100644
--- a/arquillian/arquillian-tomee-webapp-remote/src/main/java/org/apache/tomee/arquillian/webapp/TomEEWebappContainer.java
+++ b/arquillian/arquillian-tomee-webapp-remote/src/main/java/org/apache/tomee/arquillian/webapp/TomEEWebappContainer.java
@@ -1,241 +1,248 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.tomee.arquillian.webapp;
-
-import org.apache.openejb.arquillian.common.ArquillianFilterRunner;
-import org.apache.openejb.arquillian.common.Files;
-import org.apache.openejb.arquillian.common.IO;
-import org.apache.openejb.arquillian.common.Setup;
-import org.apache.openejb.arquillian.common.TomEEContainer;
-import org.apache.openejb.arquillian.common.Zips;
-import org.apache.openejb.config.RemoteServer;
-import org.apache.tomee.installer.Installer;
-import org.apache.tomee.installer.Paths;
-import org.jboss.arquillian.container.spi.client.container.LifecycleException;
-import org.jboss.arquillian.protocol.servlet.ServletMethodExecutor;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
-import java.net.URLConnection;
-import java.util.Arrays;
-import java.util.Map;
-import java.util.TreeMap;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-/*
- * TODO: delete old embedded adapter, move the tests and set those up
- */
-public class TomEEWebappContainer extends TomEEContainer<TomEEWebappConfiguration> {
-
- private static final Logger logger = Logger.getLogger(TomEEWebappContainer.class.getName());
-
- private RemoteServer container;
- private boolean shutdown;
- private File openejbHome;
- private boolean wereOpenejbHomeSet = true;
-
- @Override
- public void start() throws LifecycleException {
- // see if TomEE is already running by checking the http port
- final int httpPort = configuration.getHttpPort();
- if (Setup.isRunning(configuration.getHost(), httpPort)) {
-
- logger.info(String.format("Tomcat found running on port %s", httpPort));
-
- return;
- }
-
- shutdown = true;
-
- final String s = File.separator;
- try {
- final File workingDirectory = new File(configuration.getDir());
-
- if (workingDirectory.exists()) {
-
- Files.assertDir(workingDirectory);
-
- } else {
-
- Files.mkdir(workingDirectory);
- Files.deleteOnExit(workingDirectory);
- }
-
- Files.readable(workingDirectory);
- Files.writable(workingDirectory);
-
- openejbHome = Setup.findHome(workingDirectory);
- Installer installer = null;
-
- if (openejbHome == null) {
-
- downloadTomcat(workingDirectory, configuration.getTomcatVersion());
-
- openejbHome = Setup.findHome(workingDirectory);
-
- Files.deleteOnExit(openejbHome);
-
- final File webapp = new File(openejbHome, "webapps" + s + "tomee");
-
- Files.mkdir(webapp);
- downloadOpenEJBWebapp(webapp);
-
- System.setProperty("catalina.home", openejbHome.getAbsolutePath());
- System.setProperty("catalina.base", openejbHome.getAbsolutePath());
-
- System.setProperty("openejb.deploymentId.format", System.getProperty("openejb.deploymentId.format", "{appId}/{ejbJarId}/{ejbName}"));
-
- final Paths paths = new Paths(webapp);
- installer = new Installer(paths, true);
- if (!configuration.isUseInstallerServlet()) {
- installer.installAll();
-
- }
-
- wereOpenejbHomeSet = false;
- }
-
- Files.assertDir(openejbHome);
- Files.readable(openejbHome);
- Files.writable(openejbHome);
-
- Setup.configureServerXml(openejbHome, configuration);
- Setup.configureSystemProperties(openejbHome, configuration);
-
- Setup.exportProperties(openejbHome, configuration, true);
-
- final URL logging = Thread.currentThread().getContextClassLoader().getResource("default.remote.logging.properties");
- if (logging != null) {
- write(logging, new File(openejbHome, "conf" + s + "logging.properties"));
- }
-
- if (configuration.isRemoveUnusedWebapps()) {
- Setup.removeUselessWebapps(openejbHome, "tomee");
- }
-
- if (logger.isLoggable(Level.FINE)) {
- final Map<Object, Object> map = new TreeMap<Object, Object>(System.getProperties());
- for (final Map.Entry<Object, Object> entry : map.entrySet()) {
- System.out.printf("%s = %s\n", entry.getKey(), entry.getValue());
- }
- }
-
- Setup.installArquillianBeanDiscoverer(openejbHome);
-
- if (!wereOpenejbHomeSet && configuration.isUseInstallerServlet()) {
- // instead of calling the Installer, let's just do like users do
- // call the servlet installer instead
- final String baseUrl = "http://" + configuration.getHost() + ":" + httpPort + "/tomee/installer";
-
- assert installer != null;
- installer.addTomEEAdminConfInTomcatUsers(true);
-
- final RemoteServer tmpContainer = new RemoteServer();
- tmpContainer.setPortStartup(httpPort);
- tmpContainer.start();
-
- final URL url = new URL(baseUrl);
- logger.info("Calling TomEE Installer Servlet on " + url);
-
- for (int i = 0; i < 3; i++) {
- final URLConnection uc = url.openConnection();
- // dG9tZWU6dG9tZWU= --> Base64 of tomee:tomee
- final String authorizationString = "Basic dG9tZWU6dG9tZWU=";
- uc.setRequestProperty("Authorization", authorizationString);
- try {
- final InputStream is = uc.getInputStream();
- org.apache.openejb.loader.IO.slurp(is);
- is.close();
- break;
- } catch (final Exception e) {
- logger.warning(e.getMessage());
- Thread.sleep(1000);
- }
- }
-
- tmpContainer.stop();
- tmpContainer.getServer().waitFor();
- }
-
- container = new RemoteServer();
- container.setPortStartup(httpPort);
- container.start(Arrays.asList("-Dorg.apache.openejb.servlet.filters=" + ArquillianFilterRunner.class.getName() + "=" + ServletMethodExecutor.ARQUILLIAN_SERVLET_MAPPING), "start", true);
- container.killOnExit();
- } catch (final Exception e) {
- throw new LifecycleException("Unable to start remote container", e);
- }
- }
-
- private static void write(final URL resource, final File file) throws IOException {
- if (file.exists()) {
- Files.delete(file);
- }
- final InputStream is = org.apache.openejb.loader.IO.read(resource);
-
- try {
- IO.copy(is, file);
- } finally {
- is.close();
- }
- }
-
- protected void downloadOpenEJBWebapp(final File targetDirectory) throws LifecycleException {
- final String artifactName = configuration.getArtifactName();
- final File zipFile = Setup.downloadFile(artifactName, null);
- Zips.unzip(zipFile, targetDirectory);
- }
-
- protected void downloadTomcat(final File catalinaDirectory, final String tomcatVersion) throws LifecycleException {
- String source = null;
-
- if (tomcatVersion.startsWith("7.")) {
- source = "http://archive.apache.org/dist/tomcat/tomcat-7/v" + tomcatVersion + "/bin/apache-tomcat-" + tomcatVersion + ".zip";
- }
-
- if (tomcatVersion.startsWith("6.")) {
- source = "http://archive.apache.org/dist/tomcat/tomcat-6/v" + tomcatVersion + "/bin/apache-tomcat-" + tomcatVersion + ".zip";
- }
-
- if (tomcatVersion.startsWith("5.5")) {
- source = "http://archive.apache.org/dist/tomcat/tomcat-5/v" + tomcatVersion + "/bin/apache-tomcat-" + tomcatVersion + ".zip";
- }
-
- if (source == null) {
- throw new LifecycleException("Unable to find URL for Tomcat " + tomcatVersion);
- }
-
- final File zipFile = Setup.downloadFile("org.apache.tomcat:tomcat:" + tomcatVersion + ":zip", source);
- Zips.unzip(zipFile, catalinaDirectory);
- }
-
- @Override
- public void stop() throws LifecycleException {
- // only stop the container if we started it
- if (shutdown) {
- Setup.removeArquillianBeanDiscoverer(openejbHome);
- container.destroy();
- }
- }
-
- @Override
- public Class<TomEEWebappConfiguration> getConfigurationClass() {
- return TomEEWebappConfiguration.class;
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomee.arquillian.webapp;
+
+import org.apache.openejb.OpenEJBRuntimeException;
+import org.apache.openejb.arquillian.common.ArquillianFilterRunner;
+import org.apache.openejb.arquillian.common.Files;
+import org.apache.openejb.arquillian.common.IO;
+import org.apache.openejb.arquillian.common.Setup;
+import org.apache.openejb.arquillian.common.TomEEContainer;
+import org.apache.openejb.arquillian.common.Zips;
+import org.apache.openejb.config.RemoteServer;
+import org.apache.tomee.installer.Installer;
+import org.apache.tomee.installer.Paths;
+import org.jboss.arquillian.container.spi.client.container.LifecycleException;
+import org.jboss.arquillian.protocol.servlet.ServletMethodExecutor;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Arrays;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/*
+ * TODO: delete old embedded adapter, move the tests and set those up
+ */
+public class TomEEWebappContainer extends TomEEContainer<TomEEWebappConfiguration> {
+
+ private static final Logger logger = Logger.getLogger(TomEEWebappContainer.class.getName());
+
+ private RemoteServer container;
+ private boolean shutdown;
+ private File openejbHome;
+ private boolean wereOpenejbHomeSet = true;
+
+ @Override
+ public void start() throws LifecycleException {
+ // see if TomEE is already running by checking the http port
+ final int httpPort = configuration.getHttpPort();
+ if (Setup.isRunning(configuration.getHost(), httpPort)) {
+
+ logger.info(String.format("Tomcat found running on port %s", httpPort));
+
+ return;
+ }
+
+ shutdown = true;
+
+ final String s = File.separator;
+ try {
+ final File workingDirectory = new File(configuration.getDir());
+
+ if (workingDirectory.exists()) {
+
+ Files.assertDir(workingDirectory);
+
+ } else {
+
+ Files.mkdir(workingDirectory);
+ Files.deleteOnExit(workingDirectory);
+ }
+
+ Files.readable(workingDirectory);
+ Files.writable(workingDirectory);
+
+ openejbHome = Setup.findHome(workingDirectory);
+ Installer installer = null;
+
+ if (openejbHome == null) {
+
+ downloadTomcat(workingDirectory, configuration.getTomcatVersion());
+
+ openejbHome = Setup.findHome(workingDirectory);
+
+ Files.deleteOnExit(openejbHome);
+
+ final File webapp = new File(openejbHome, "webapps" + s + "tomee");
+
+ Files.mkdir(webapp);
+ downloadOpenEJBWebapp(webapp);
+
+ System.setProperty("catalina.home", openejbHome.getAbsolutePath());
+ System.setProperty("catalina.base", openejbHome.getAbsolutePath());
+
+ System.setProperty("openejb.deploymentId.format", System.getProperty("openejb.deploymentId.format", "{appId}/{ejbJarId}/{ejbName}"));
+
+ final Paths paths = new Paths(webapp);
+ installer = new Installer(paths, true);
+ if (!configuration.isUseInstallerServlet()) {
+ installer.installAll();
+
+ }
+
+ wereOpenejbHomeSet = false;
+ }
+
+ Files.assertDir(openejbHome);
+ Files.readable(openejbHome);
+ Files.writable(openejbHome);
+
+ Setup.configureServerXml(openejbHome, configuration);
+ Setup.configureSystemProperties(openejbHome, configuration);
+
+ Setup.exportProperties(openejbHome, configuration, true);
+
+ final URL logging = Thread.currentThread().getContextClassLoader().getResource("default.remote.logging.properties");
+ if (logging != null) {
+ write(logging, new File(openejbHome, "conf" + s + "logging.properties"));
+ }
+
+ if (configuration.isRemoveUnusedWebapps()) {
+ Setup.removeUselessWebapps(openejbHome, "tomee");
+ }
+
+ if (logger.isLoggable(Level.FINE)) {
+ final Map<Object, Object> map = new TreeMap<Object, Object>(System.getProperties());
+ for (final Map.Entry<Object, Object> entry : map.entrySet()) {
+ System.out.printf("%s = %s\n", entry.getKey(), entry.getValue());
+ }
+ }
+
+ Setup.installArquillianBeanDiscoverer(openejbHome);
+
+ if (!wereOpenejbHomeSet && configuration.isUseInstallerServlet()) {
+ // instead of calling the Installer, let's just do like users do
+ // call the servlet installer instead
+ final String baseUrl = "http://" + configuration.getHost() + ":" + httpPort + "/tomee/installer";
+
+ assert installer != null;
+ installer.addTomEEAdminConfInTomcatUsers(true);
+
+ final RemoteServer tmpContainer = new RemoteServer();
+ tmpContainer.setPortStartup(httpPort);
+ try {
+ tmpContainer.start();
+ } catch (final Exception e) {
+ tmpContainer.destroy();
+ throw e;
+ }
+
+ final URL url = new URL(baseUrl);
+ logger.info("Calling TomEE Installer Servlet on " + url);
+
+ for (int i = 0; i < 3; i++) {
+ final URLConnection uc = url.openConnection();
+ // dG9tZWU6dG9tZWU= --> Base64 of tomee:tomee
+ final String authorizationString = "Basic dG9tZWU6dG9tZWU=";
+ uc.setRequestProperty("Authorization", authorizationString);
+ try {
+ final InputStream is = uc.getInputStream();
+ org.apache.openejb.loader.IO.slurp(is);
+ is.close();
+ break;
+ } catch (final Exception e) {
+ logger.warning(e.getMessage());
+ Thread.sleep(1000);
+ }
+ }
+
+ tmpContainer.stop();
+ tmpContainer.getServer().waitFor();
+ }
+
+ container = new RemoteServer();
+ container.setPortStartup(httpPort);
+ container.start(Arrays.asList("-Dorg.apache.openejb.servlet.filters=" + ArquillianFilterRunner.class.getName() + "=" + ServletMethodExecutor.ARQUILLIAN_SERVLET_MAPPING), "start", true);
+ container.killOnExit();
+ } catch (final Exception e) {
+ container.destroy();
+ throw new LifecycleException("Unable to start remote container", e);
+ }
+ }
+
+ private static void write(final URL resource, final File file) throws IOException {
+ if (file.exists()) {
+ Files.delete(file);
+ }
+ final InputStream is = org.apache.openejb.loader.IO.read(resource);
+
+ try {
+ IO.copy(is, file);
+ } finally {
+ is.close();
+ }
+ }
+
+ protected void downloadOpenEJBWebapp(final File targetDirectory) throws LifecycleException {
+ final String artifactName = configuration.getArtifactName();
+ final File zipFile = Setup.downloadFile(artifactName, null);
+ Zips.unzip(zipFile, targetDirectory);
+ }
+
+ protected void downloadTomcat(final File catalinaDirectory, final String tomcatVersion) throws LifecycleException {
+ String source = null;
+
+ if (tomcatVersion.startsWith("7.")) {
+ source = "http://archive.apache.org/dist/tomcat/tomcat-7/v" + tomcatVersion + "/bin/apache-tomcat-" + tomcatVersion + ".zip";
+ }
+
+ if (tomcatVersion.startsWith("6.")) {
+ source = "http://archive.apache.org/dist/tomcat/tomcat-6/v" + tomcatVersion + "/bin/apache-tomcat-" + tomcatVersion + ".zip";
+ }
+
+ if (tomcatVersion.startsWith("5.5")) {
+ source = "http://archive.apache.org/dist/tomcat/tomcat-5/v" + tomcatVersion + "/bin/apache-tomcat-" + tomcatVersion + ".zip";
+ }
+
+ if (source == null) {
+ throw new LifecycleException("Unable to find URL for Tomcat " + tomcatVersion);
+ }
+
+ final File zipFile = Setup.downloadFile("org.apache.tomcat:tomcat:" + tomcatVersion + ":zip", source);
+ Zips.unzip(zipFile, catalinaDirectory);
+ }
+
+ @Override
+ public void stop() throws LifecycleException {
+ // only stop the container if we started it
+ if (shutdown) {
+ Setup.removeArquillianBeanDiscoverer(openejbHome);
+ container.destroy();
+ }
+ }
+
+ @Override
+ public Class<TomEEWebappConfiguration> getConfigurationClass() {
+ return TomEEWebappConfiguration.class;
+ }
+}