Author: dain
Date: Thu Oct 4 12:24:21 2007
New Revision: 581991
URL: http://svn.apache.org/viewvc?rev=581991&view=rev
Log:
Tomcat installer now completely optional
Added:
openejb/trunk/openejb3/container/openejb-javaagent/src/test/
openejb/trunk/openejb3/container/openejb-javaagent/src/test/java/
openejb/trunk/openejb3/container/openejb-javaagent/src/test/java/org/
openejb/trunk/openejb3/container/openejb-javaagent/src/test/java/org/apache/
openejb/trunk/openejb3/container/openejb-javaagent/src/test/java/org/apache/openejb/
openejb/trunk/openejb3/container/openejb-javaagent/src/test/java/org/apache/openejb/javaagent/
openejb/trunk/openejb3/container/openejb-javaagent/src/test/java/org/apache/openejb/javaagent/AgentTest.java
Modified:
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/OpenEJBNamingContextListener.java
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/TomcatJndiBuilder.java
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/TomcatLoader.java
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/TomcatWebAppBuilder.java
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/installer/Installer.java
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/WEB-INF/web.xml
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/index.jsp
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/installer-view.jsp
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/testejb.jsp
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/testhome.jsp
openejb/trunk/openejb3/container/openejb-javaagent/pom.xml
openejb/trunk/openejb3/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java
openejb/trunk/openejb3/container/openejb-loader/src/main/java/org/apache/openejb/loader/OpenEJBListener.java
Modified:
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/OpenEJBNamingContextListener.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/OpenEJBNamingContextListener.java?rev=581991&r1=581990&r2=581991&view=diff
==============================================================================
---
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/OpenEJBNamingContextListener.java
(original)
+++
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/OpenEJBNamingContextListener.java
Thu Oct 4 12:24:21 2007
@@ -50,9 +50,9 @@
private final StandardServer standardServer;
/**
- * Initialized flag.
+ * Has the listener been started?
*/
- private boolean initialized = false;
+ private boolean running = false;
/**
* Associated naming resources.
@@ -70,26 +70,38 @@
}
if (event.getType() == Lifecycle.START_EVENT) {
- if (initialized) return;
+ start();
- namingResources.addPropertyChangeListener(this);
- processInitialNamingResources();
+ } else if (event.getType() == Lifecycle.STOP_EVENT) {
+ stop();
+ }
+ }
- initialized = true;
+ public boolean isRunning() {
+ return running;
+ }
- } else if (event.getType() == Lifecycle.STOP_EVENT) {
+ public void start() {
+ if (running) return;
- if (!initialized) return;
+ namingResources.addPropertyChangeListener(this);
+ processInitialNamingResources();
- namingResources.removePropertyChangeListener(this);
- initialized = false;
- }
+ running = true;
+ }
+
+ public void stop() {
+ if (!running) return;
+
+ namingResources.removePropertyChangeListener(this);
+
+ running = false;
}
public void propertyChange(PropertyChangeEvent event) {
- if (!initialized) return;
+ if (!running) return;
Object source = event.getSource();
if (source == namingResources) {
Modified:
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/TomcatJndiBuilder.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/TomcatJndiBuilder.java?rev=581991&r1=581990&r2=581991&view=diff
==============================================================================
---
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/TomcatJndiBuilder.java
(original)
+++
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/TomcatJndiBuilder.java
Thu Oct 4 12:24:21 2007
@@ -18,6 +18,7 @@
package org.apache.openejb.tomcat;
import org.apache.catalina.core.StandardContext;
+import org.apache.catalina.core.NamingContextListener;
import org.apache.catalina.deploy.ContextEjb;
import org.apache.catalina.deploy.ContextEnvironment;
import org.apache.catalina.deploy.ContextResource;
@@ -25,6 +26,7 @@
import org.apache.catalina.deploy.ContextTransaction;
import org.apache.catalina.deploy.NamingResources;
import org.apache.naming.factory.Constants;
+import org.apache.naming.ContextAccessController;
import org.apache.openejb.OpenEJBException;
import org.apache.openejb.assembler.classic.EjbLocalReferenceInfo;
import org.apache.openejb.assembler.classic.EjbReferenceInfo;
@@ -60,12 +62,18 @@
private final StandardContext standardContext;
private final WebAppInfo webAppInfo;
private final LinkResolver<EntityManagerFactory> emfLinkResolver;
+ private final boolean replaceEntry;
private boolean useCrossClassLoaderRef = false;
+ private NamingContextListener namingContextListener;
public TomcatJndiBuilder(StandardContext standardContext, WebAppInfo
webAppInfo, LinkResolver<EntityManagerFactory> emfLinkResolver) {
this.standardContext = standardContext;
+ this.namingContextListener =
standardContext.getNamingContextListener();
this.webAppInfo = webAppInfo;
this.emfLinkResolver = emfLinkResolver;
+
+ String parameter = standardContext.findParameter("openejb.start.late");
+ replaceEntry = Boolean.parseBoolean(parameter);
}
public boolean isUseCrossClassLoaderRef() {
@@ -130,6 +138,13 @@
if (addEntry) {
naming.addEnvironment(environment);
}
+
+ if (replaceEntry) {
+
ContextAccessController.setWritable(namingContextListener.getName(),
standardContext);
+ if (!addEntry)
namingContextListener.removeEnvironment(environment.getName());
+ namingContextListener.addEnvironment(environment);
+
ContextAccessController.setReadOnly(namingContextListener.getName());
+ }
}
public void mergeRef(NamingResources naming, EjbReferenceInfo ref) {
@@ -163,6 +178,13 @@
if (addEntry) {
naming.addEjb(ejb);
}
+
+ if (replaceEntry) {
+
ContextAccessController.setWritable(namingContextListener.getName(),
standardContext);
+ if (!addEntry) namingContextListener.removeEjb(ejb.getName());
+ namingContextListener.addEjb(ejb);
+
ContextAccessController.setReadOnly(namingContextListener.getName());
+ }
}
public void mergeRef(NamingResources naming, EjbLocalReferenceInfo ref) {
@@ -195,6 +217,13 @@
if (addEntry) {
naming.addEjb(ejb);
}
+
+ if (replaceEntry) {
+
ContextAccessController.setWritable(namingContextListener.getName(),
standardContext);
+ if (!addEntry) namingContextListener.removeEjb(ejb.getName());
+ namingContextListener.addEjb(ejb);
+
ContextAccessController.setReadOnly(namingContextListener.getName());
+ }
}
public void mergeRef(NamingResources naming,
PersistenceContextReferenceInfo ref, URI moduleUri) {
@@ -235,6 +264,13 @@
if (addEntry) {
naming.addResource(resource);
}
+
+ if (replaceEntry) {
+
ContextAccessController.setWritable(namingContextListener.getName(),
standardContext);
+ if (!addEntry)
namingContextListener.removeResource(resource.getName());
+ namingContextListener.addResource(resource);
+
ContextAccessController.setReadOnly(namingContextListener.getName());
+ }
}
public void mergeRef(NamingResources naming, PersistenceUnitReferenceInfo
ref, URI moduleUri) {
@@ -268,6 +304,13 @@
if (addEntry) {
naming.addResource(resource);
}
+
+ if (replaceEntry) {
+
ContextAccessController.setWritable(namingContextListener.getName(),
standardContext);
+ if (!addEntry)
namingContextListener.removeResource(resource.getName());
+ namingContextListener.addResource(resource);
+
ContextAccessController.setReadOnly(namingContextListener.getName());
+ }
}
public void mergeRef(NamingResources naming, ResourceReferenceInfo ref) {
@@ -300,6 +343,13 @@
if (addEntry) {
naming.addResource(resource);
}
+
+ if (replaceEntry) {
+
ContextAccessController.setWritable(namingContextListener.getName(),
standardContext);
+ if (!addEntry)
namingContextListener.removeResource(resource.getName());
+ namingContextListener.addResource(resource);
+
ContextAccessController.setReadOnly(namingContextListener.getName());
+ }
}
public void mergeRef(NamingResources naming, ResourceEnvReferenceInfo ref)
{
@@ -330,6 +380,13 @@
if (addEntry) {
naming.addResourceEnvRef(resourceEnv);
+ }
+
+ if (replaceEntry) {
+
ContextAccessController.setWritable(namingContextListener.getName(),
standardContext);
+ if (!addEntry)
namingContextListener.removeResourceEnvRef(resourceEnv.getName());
+ namingContextListener.addResourceEnvRef(resourceEnv);
+
ContextAccessController.setReadOnly(namingContextListener.getName());
}
}
Modified:
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/TomcatLoader.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/TomcatLoader.java?rev=581991&r1=581990&r2=581991&view=diff
==============================================================================
---
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/TomcatLoader.java
(original)
+++
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/TomcatLoader.java
Thu Oct 4 12:24:21 2007
@@ -25,6 +25,13 @@
import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.server.ServiceException;
import org.apache.openejb.server.ejbd.EjbServer;
+import org.apache.catalina.core.StandardServer;
+import org.apache.catalina.core.StandardContext;
+import org.apache.catalina.Service;
+import org.apache.catalina.Engine;
+import org.apache.catalina.Container;
+import org.apache.catalina.Host;
+import org.apache.catalina.ServerFactory;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
@@ -47,22 +54,76 @@
return;
}
+ // initialize system instance before doing anything
System.setProperty("openejb.provider.default",
"org.apache.openejb.tomcat");
-
+ SystemInstance.init(props);
+
+ // Install tomcat thread context listener
ThreadContext.addThreadContextListener(new
TomcatThreadContextListener());
- if (SystemInstance.get().getComponent(WebAppBuilder.class) == null) {
- TomcatWebAppBuilder tomcatWebAppBuilder = new
TomcatWebAppBuilder();
+ // Install tomcat war builder
+ TomcatWebAppBuilder tomcatWebAppBuilder = (TomcatWebAppBuilder)
SystemInstance.get().getComponent(WebAppBuilder.class);
+ if (tomcatWebAppBuilder == null) {
+ tomcatWebAppBuilder = new TomcatWebAppBuilder();
tomcatWebAppBuilder.start();
SystemInstance.get().setComponent(WebAppBuilder.class,
tomcatWebAppBuilder);
}
- SystemInstance.init(props);
-
+ // Start OpenEJB
ejbServer = new EjbServer();
SystemInstance.get().setComponent(EjbServer.class, ejbServer);
OpenEJB.init(props, new ServerFederation());
ejbServer.init(props);
+
+ // Add our naming context listener to the server which registers all
Tomcat resources with OpenEJB
+ StandardServer standardServer = (StandardServer)
ServerFactory.getServer();
+ OpenEJBNamingContextListener namingContextListener = new
OpenEJBNamingContextListener(standardServer);
+ // Standard server has no state property, so we check global naming
context to determine if server is started yet
+ if (standardServer.getGlobalNamingContext() != null) {
+ namingContextListener.start();
+ }
+ standardServer.addLifecycleListener(namingContextListener);
+
+ // Process all applications already started. This deploys EJBs,
PersistenceUnits
+ // and modifies JNDI ENC references to OpenEJB managed objects such as
EJBs.
+ processRunningApplications(tomcatWebAppBuilder, standardServer);
+ }
+
+ private void processRunningApplications(TomcatWebAppBuilder
tomcatWebAppBuilder, StandardServer standardServer) {
+ for (Service service : standardServer.findServices()) {
+ if (service.getContainer() instanceof Engine) {
+ Engine engine = (Engine) service.getContainer();
+ for (Container engineChild : engine.findChildren()) {
+ if (engineChild instanceof Host) {
+ Host host = (Host) engineChild;
+ for (Container hostChild : host.findChildren()) {
+ if (hostChild instanceof StandardContext) {
+ StandardContext standardContext =
(StandardContext) hostChild;
+ int state = standardContext.getState();
+ if (state == 0) {
+ // context only initialized
+ tomcatWebAppBuilder.init(standardContext);
+ } else if (state == 1) {
+ // context started
+
standardContext.addParameter("openejb.start.late", "true");
+ ClassLoader oldCL =
Thread.currentThread().getContextClassLoader();
+
Thread.currentThread().setContextClassLoader(standardContext.getLoader().getClassLoader());
+ try {
+
tomcatWebAppBuilder.init(standardContext);
+
tomcatWebAppBuilder.beforeStart(standardContext);
+
tomcatWebAppBuilder.start(standardContext);
+
tomcatWebAppBuilder.afterStart(standardContext);
+ } finally {
+
Thread.currentThread().setContextClassLoader(oldCL);
+ }
+
standardContext.removeParameter("openejb.start.late");
+ }
+ }
+ }
+ }
+ }
+ }
+ }
}
public void service(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
Modified:
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/TomcatWebAppBuilder.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/TomcatWebAppBuilder.java?rev=581991&r1=581990&r2=581991&view=diff
==============================================================================
---
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/TomcatWebAppBuilder.java
(original)
+++
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/TomcatWebAppBuilder.java
Thu Oct 4 12:24:21 2007
@@ -76,8 +76,6 @@
public TomcatWebAppBuilder() {
StandardServer standardServer = (StandardServer)
ServerFactory.getServer();
- standardServer.addLifecycleListener(new
OpenEJBNamingContextListener(standardServer));
-
globalListenerSupport = new GlobalListenerSupport(standardServer,
this);
// MBeanServer mbeanServer;
@@ -94,6 +92,7 @@
public void start() {
globalListenerSupport.start();
+
}
public void stop() {
@@ -126,6 +125,7 @@
Assembler assembler = getAssembler();
if (assembler == null) {
logger.warning("OpenEJB has not been initialized so war will not
be scanned for nested modules " + standardContext.getPath());
+ return;
}
AppModule appModule = loadApplication(standardContext);
@@ -153,8 +153,8 @@
// bound after the context is created
ContextAccessController.setWritable(standardContext.getNamingContextListener().getName(),
standardContext);
try {
- Context comp = (Context)
ContextBindings.getThread().lookup("comp");
-
+ Context comp = (Context)
ContextBindings.getClassLoader().lookup("comp");
+
// bind TransactionManager
TransactionManager transactionManager =
SystemInstance.get().getComponent(TransactionManager.class);
safeBind(comp, "TransactionManager", transactionManager);
Modified:
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/installer/Installer.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/installer/Installer.java?rev=581991&r1=581990&r2=581991&view=diff
==============================================================================
---
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/installer/Installer.java
(original)
+++
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/java/org/apache/openejb/tomcat/installer/Installer.java
Thu Oct 4 12:24:21 2007
@@ -40,12 +40,30 @@
NONE, INSTALLED, REBOOT_REQUIRED
}
+ private static final boolean listenerInstalled;
+ private static final boolean agentInstalled;
+
+ static {
+ // is the OpenEJB listener installed
+ Boolean listener = (Boolean)
invokeStaticNoArgMethod("org.apache.openejb.loader.OpenEJBListener",
"isListenerInstalled");
+ if (listener == null) listener = false;
+ listenerInstalled = listener;
+
+ // is the OpenEJB javaagent installed
+ agentInstalled =
invokeStaticNoArgMethod("org.apache.openejb.javaagent.Agent",
"getInstrumentation") != null;
+ }
+
+ public static boolean isListenerInstalled() {
+ return listenerInstalled;
+ }
+
+ public static boolean isAgentInstalled() {
+ return agentInstalled;
+ }
+
private final Paths paths;
private Status status = Status.NONE;
- private final boolean listenerInstalled;
- private final boolean agentInstalled;
-
// Thi may need to be redesigned but the goal is to provide some feedback
on what happened
private final List<String> errors = new ArrayList<String>();
private final List<String> warnings = new ArrayList<String>();
@@ -54,28 +72,11 @@
public Installer(Paths paths) {
this.paths = paths;
- // is the OpenEJB listener installed
- Boolean listenerInstalled = (Boolean)
invokeStaticNoArgMethod("org.apache.openejb.loader.OpenEJBListener",
"isInstalled");
- if (listenerInstalled == null) listenerInstalled = false;
- this.listenerInstalled = listenerInstalled;
-
- // is the OpenEJB javaagent installed
- agentInstalled =
invokeStaticNoArgMethod("org.apache.openejb.javaagent.Agent",
"getInstrumentation") != null;
-
if (listenerInstalled && agentInstalled) {
status = Status.INSTALLED;
}
}
-
- public boolean isListenerInstalled() {
- return listenerInstalled;
- }
-
- public boolean isAgentInstalled() {
- return agentInstalled;
- }
-
public Status getStatus() {
return status;
}
@@ -420,9 +421,9 @@
// return tomcatVersion;
// }
- private Object invokeStaticNoArgMethod(String className, String
propertyName) {
+ private static Object invokeStaticNoArgMethod(String className, String
propertyName) {
try {
- Class<?> clazz = loadClass(className, getClass().getClassLoader());
+ Class<?> clazz = loadClass(className,
Installer.class.getClassLoader());
Method method = clazz.getMethod(propertyName);
Object result = method.invoke(null, (Object[]) null);
return result;
@@ -431,7 +432,7 @@
}
}
- private Class<?> loadClass(String className, ClassLoader classLoader)
throws ClassNotFoundException {
+ private static Class<?> loadClass(String className, ClassLoader
classLoader) throws ClassNotFoundException {
LinkedList<ClassLoader> loaders = new LinkedList<ClassLoader>();
for (ClassLoader loader = classLoader; loader != null; loader =
loader.getParent()) {
loaders.addFirst(loader);
@@ -521,5 +522,4 @@
private void addInfo(String message) {
infos.add(message);
}
-
}
Modified:
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/WEB-INF/web.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/WEB-INF/web.xml?rev=581991&r1=581990&r2=581991&view=diff
==============================================================================
---
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/WEB-INF/web.xml
(original)
+++
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/WEB-INF/web.xml
Thu Oct 4 12:24:21 2007
@@ -32,6 +32,7 @@
<servlet>
<servlet-name>InstallerServlet</servlet-name>
<servlet-class>org.apache.openejb.tomcat.installer.InstallerServlet</servlet-class>
+ <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
Modified:
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/index.jsp
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/index.jsp?rev=581991&r1=581990&r2=581991&view=diff
==============================================================================
--- openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/index.jsp
(original)
+++ openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/index.jsp
Thu Oct 4 12:24:21 2007
@@ -1,3 +1,4 @@
+<%@ page import="org.apache.openejb.tomcat.installer.Installer" %>
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
@@ -23,7 +24,6 @@
<img border="0" height="2" width="20"
src="images/dotTrans.gif">
<a href="index.jsp"><span class="menuTopOff">Index</span></a>
<img border="0" height="2" width="20"
src="images/dotTrans.gif">
-<% if (installed) { %>
<a href="viewjndi.jsp"><span class="menuTopOff">JNDI</span></a>
<img border="0" height="2" width="20"
src="images/dotTrans.gif">
<a href="viewejb.jsp"><span class="menuTopOff">EJB</span></a>
@@ -32,7 +32,6 @@
<img border="0" height="2" width="20"
src="images/dotTrans.gif">
<a href="invokeobj.jsp"><span
class="menuTopOff">Invoke</span></a>
<img border="0" height="2" width="20"
src="images/dotTrans.gif">
-<% } %>
</td>
<td align="left" valign="top" height="20" width="120"><img
border="0" height="2" width="10" src="images/dotTrans.gif"></td>
</tr>
@@ -62,12 +61,6 @@
<p>
<FONT SIZE='2'>
<B>Welcome to the OpenEJB/Tomcat integration!</B><br><BR>
-<% if (!installed) { %>
- To complete the installation process, please click the link
below.
- <BR><BR>
- <B>Install</B><BR>
- <A HREF="installer">Install OpenEJB</A><BR>
-<% } else { %>
Now that OpenEJB has been installed, click on
the "Testing your setup" link below to verify it. When
everything
is setup well, feel free to play around with the tools
provided below!
@@ -75,6 +68,19 @@
<B>Setup</B><BR>
<A HREF="testhome.jsp">Testing your setup</A><BR>
<BR>
+<% if (!Installer.isListenerInstalled() && !Installer.isAgentInstalled()) { %>
+ <B>Install</B><BR>
+ <A HREF="installer">[Optional] Install Listener and
JavaAgent</A><BR>
+ <BR>
+<% } else if (!Installer.isListenerInstalled()) { %>
+ <B>Install</B><BR>
+ <A HREF="installer">[Optional] Install Listener</A><BR>
+ <BR>
+<% } else if (!Installer.isAgentInstalled()) { %>
+ <B>Install</B><BR>
+ <A HREF="installer">[Optional] JavaAgent</A><BR>
+ <BR>
+<% } %>
<B>Tools</B><BR>
<A HREF="viewjndi.jsp">OpenEJB JNDI Browser</A><BR>
<A HREF="viewclass.jsp">OpenEJB Class Viewer</A><BR>
@@ -88,7 +94,6 @@
<A HREF="ejbref.html">How to configure java:comp/env
lookups</A><BR>
<BR>
--%>
-<% } %>
</FONT>
</p>
<p>
@@ -103,15 +108,4 @@
</tr>
</table>
</body>
-</html>
-<%!
- private static boolean installed;
- static {
- Class clazz = null;
- try {
- clazz = Class.forName("org.apache.openejb.OpenEJB");
- } catch(Exception e) {}
- installed = (clazz != null);
- }
-
-%>
\ No newline at end of file
+</html>
\ No newline at end of file
Modified:
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/installer-view.jsp
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/installer-view.jsp?rev=581991&r1=581990&r2=581991&view=diff
==============================================================================
---
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/installer-view.jsp
(original)
+++
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/installer-view.jsp
Thu Oct 4 12:24:21 2007
@@ -30,7 +30,6 @@
<img border="0" height="2" width="20"
src="images/dotTrans.gif">
<a href="index.jsp"><span class="menuTopOff">Index</span></a>
<img border="0" height="2" width="20"
src="images/dotTrans.gif">
-<% if (installer.getStatus() == Installer.Status.INSTALLED) { %>
<a href="viewjndi.jsp"><span class="menuTopOff">JNDI</span></a>
<img border="0" height="2" width="20"
src="images/dotTrans.gif">
<a href="viewejb.jsp"><span class="menuTopOff">EJB</span></a>
@@ -39,7 +38,6 @@
<img border="0" height="2" width="20"
src="images/dotTrans.gif">
<a href="invokeobj.jsp"><span
class="menuTopOff">Invoke</span></a>
<img border="0" height="2" width="20"
src="images/dotTrans.gif">
-<% } %>
</td>
<td align="left" valign="top" height="20" width="120"><img
border="0" height="2" width="10" src="images/dotTrans.gif"></td>
</tr>
@@ -90,7 +88,7 @@
<br><br>
The installer has completed successfully. <br>
- Please, <b>restart Tomcat</b> and reload this page.
+ Please, <b>restart Tomcat</b> and reload this page to verify
installation.
<%
} else {
List<String> errors = new ArrayList<String>(paths.getErrors());
Modified:
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/testejb.jsp
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/testejb.jsp?rev=581991&r1=581990&r2=581991&view=diff
==============================================================================
---
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/testejb.jsp
(original)
+++
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/testejb.jsp
Thu Oct 4 12:24:21 2007
@@ -137,6 +137,7 @@
ejbHome = ctx.lookup("MEJB");
if (ejbHome instanceof java.rmi.Remote) out.println(OK);
} catch (Exception e) {
+ e.printStackTrace(new PrintWriter(out));
out.println(FAIL);
return;
}
Modified:
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/testhome.jsp
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/testhome.jsp?rev=581991&r1=581990&r2=581991&view=diff
==============================================================================
---
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/testhome.jsp
(original)
+++
openejb/trunk/openejb3/assembly/openejb-tomcat/src/main/resources/testhome.jsp
Thu Oct 4 12:24:21 2007
@@ -100,7 +100,7 @@
this.session = session;
this.out = out;
- out.print("<b>openejb.home = " + System.getProperty("openejb.home") +
"</b><br><br>");
+// out.print("<b>openejb.home = " + System.getProperty("openejb.home")
+ "</b><br><br>");
try {
out.print(HR);
out.print("<table width='300' cellspacing='4' cellpadding='4'
border='0'>");
@@ -127,15 +127,15 @@
if (!openejbHomeLib.exists()) handleError(BAD_HOME + homePath,
NO_LIB, INSTRUCTIONS);
out.print(OK);
- // The openejb.home there must be openejb*.jar files in the 'lib'
directory
- out.print("<tr><td><font size='2'>has openejb*
libraries</font></td> ");
- String[] libs = openejbHomeLib.list();
- boolean found = false;
- for (int i = 0; i < libs.length && !found; i++) {
- found = (libs[i].startsWith("openejb-") &&
libs[i].endsWith(".jar"));
- }
- if (!found) handleError(BAD_HOME + homePath, NO_LIBS,
INSTRUCTIONS);
- out.print(OK);
+// // The openejb.home there must be openejb*.jar files in the
'lib' directory
+// out.print("<tr><td><font size='2'>has openejb*
libraries</font></td> ");
+// String[] libs = openejbHomeLib.list();
+// boolean found = false;
+// for (int i = 0; i < libs.length && !found; i++) {
+// found = (libs[i].startsWith("openejb-") &&
libs[i].endsWith(".jar"));
+// }
+// if (!found) handleError(BAD_HOME + homePath, NO_LIBS,
INSTRUCTIONS);
+// out.print(OK);
out.print("</table>");
out.print(HR);
Modified: openejb/trunk/openejb3/container/openejb-javaagent/pom.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-javaagent/pom.xml?rev=581991&r1=581990&r2=581991&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-javaagent/pom.xml (original)
+++ openejb/trunk/openejb3/container/openejb-javaagent/pom.xml Thu Oct 4
12:24:21 2007
@@ -34,8 +34,9 @@
<archive>
<manifestEntries>
<Premain-Class>org.apache.openejb.javaagent.Agent</Premain-Class>
- <!--<Boot-Class-Path></Boot-Class-Path>-->
- <!--<Can-Redefine-Classes>true</Can-Redefine-Classes>-->
+ <Agent-Class>org.apache.openejb.javaagent.Agent</Agent-Class>
+ <Can-Redefine-Classes>true</Can-Redefine-Classes>
+ <Can-Retransform-Classes>true</Can-Retransform-Classes>
</manifestEntries>
</archive>
</configuration>
@@ -56,4 +57,10 @@
</plugin>
</plugins>
</build>
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ </dependencies>
</project>
Modified:
openejb/trunk/openejb3/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java?rev=581991&r1=581990&r2=581991&view=diff
==============================================================================
---
openejb/trunk/openejb3/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java
(original)
+++
openejb/trunk/openejb3/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java
Thu Oct 4 12:24:21 2007
@@ -17,10 +17,25 @@
*/
package org.apache.openejb.javaagent;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.io.OutputStreamWriter;
+import java.io.Closeable;
import java.lang.instrument.Instrumentation;
+import java.lang.management.ManagementFactory;
+import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Field;
+import java.lang.reflect.Method;
import java.lang.reflect.ReflectPermission;
+import java.net.JarURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
import java.security.Permission;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
public class Agent {
private static final Permission ACCESS_PERMISSION = new
ReflectPermission("suppressAccessChecks");
@@ -28,7 +43,17 @@
private static Instrumentation instrumentation;
private static boolean initialized = false;
- public static synchronized void premain(String agentArgs, Instrumentation
instrumentation) {
+ public static void premain(String agentArgs, Instrumentation
instrumentation) {
+ if (Agent.instrumentation != null) return;
+
+ Agent.agentArgs = agentArgs;
+ Agent.instrumentation = instrumentation;
+ initialized = true;
+ }
+
+ public static void agentmain(String agentArgs, Instrumentation
instrumentation) {
+ if (Agent.instrumentation != null) return;
+
Agent.agentArgs = agentArgs;
Agent.instrumentation = instrumentation;
initialized = true;
@@ -56,21 +81,8 @@
private static synchronized void checkInitialization() {
if (!initialized) {
try {
- Class<?> systemAgentClass = null;
- try {
- ClassLoader systemCl = ClassLoader.getSystemClassLoader();
- systemAgentClass =
systemCl.loadClass(Agent.class.getName());
- } catch (ClassNotFoundException e) {
- return;
- }
-
- Field instrumentationField =
systemAgentClass.getDeclaredField("instrumentation");
- instrumentationField.setAccessible(true);
- instrumentation = (Instrumentation)
instrumentationField.get(null);
-
- Field agentArgsField =
systemAgentClass.getDeclaredField("agentArgs");
- agentArgsField.setAccessible(true);
- agentArgs = (String) agentArgsField.get(null);
+ checkSystemClassPath();
+ dynamicLoadAgent();
} catch (Exception e) {
new IllegalStateException("Unable to initialize agent",
e).printStackTrace();
} finally {
@@ -79,4 +91,137 @@
}
}
+ private static void checkSystemClassPath() throws NoSuchFieldException,
IllegalAccessException {
+ if (instrumentation != null) return;
+
+ Class<?> systemAgentClass = null;
+ try {
+ ClassLoader systemCl = ClassLoader.getSystemClassLoader();
+ systemAgentClass = systemCl.loadClass(Agent.class.getName());
+ } catch (ClassNotFoundException e) {
+ // java-agent jar was not on the system class path
+ return;
+ }
+
+ Field instrumentationField =
systemAgentClass.getDeclaredField("instrumentation");
+ instrumentationField.setAccessible(true);
+ instrumentation = (Instrumentation) instrumentationField.get(null);
+
+ Field agentArgsField = systemAgentClass.getDeclaredField("agentArgs");
+ agentArgsField.setAccessible(true);
+ agentArgs = (String) agentArgsField.get(null);
+ }
+
+ private static void dynamicLoadAgent() throws Exception{
+ if (instrumentation != null) return;
+
+ try {
+ Class<?> vmClass =
Class.forName("com.sun.tools.attach.VirtualMachine");
+ Method attachMethod = vmClass.getMethod("attach", String.class);
+ Method loadAgentMethod = vmClass.getMethod("loadAgent",
String.class);
+
+ // find the agentJar
+ String agentPath = getAgentJar();
+
+ // get the pid of the current process (for attach command)
+ String pid = getPid();
+
+ // attach to the vm
+ Object vm = attachMethod.invoke(null, new String[] { pid });
+
+ // load our agent
+ loadAgentMethod.invoke(vm, agentPath);
+
+ // The AgentJar is loaded into the system classpath, and this
class could
+ // be in a child classloader, so we need to double check the
system classpath
+ checkSystemClassPath();
+ } catch (ClassNotFoundException e) {
+ // not a Sun VM
+ } catch (NoSuchMethodException e) {
+ // not a Sun VM
+ }
+ }
+
+ private static String getPid() {
+ // This relies on the undocumented convention of the
+ // RuntimeMXBean's name starting with the PID, but
+ // there appears to be no other way to obtain the
+ // current process' id, which we need for the attach
+ // process
+ RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
+ String pid = bean.getName();
+ if (pid.indexOf("@") != -1) {
+ pid = pid.substring(0, pid.indexOf("@"));
+ }
+ return pid;
+ }
+
+ /**
+ * Try to find the openejb-javaagent jar, and if not found create a new jar
+ * file for the sole purpose of specifying an Agent-Class to load into the
JVM.
+ */
+ private static String getAgentJar() throws IOException {
+ URL resource =
Agent.class.getClassLoader().getResource(Agent.class.getName().replace('.',
'/') + ".class");
+ if (resource == null) {
+ throw new IllegalStateException("Could not find Agent class file
in class path");
+ }
+
+ URLConnection urlConnection = resource.openConnection();
+ if (urlConnection instanceof JarURLConnection) {
+ JarURLConnection jarURLConnection = (JarURLConnection)
urlConnection;
+ return jarURLConnection.getJarFile().getName();
+ }
+
+ InputStream in = urlConnection.getInputStream();
+ ZipOutputStream out = null;
+ File file = null;
+ try {
+ file = File.createTempFile(Agent.class.getName(), ".jar");
+ file.deleteOnExit();
+
+ out = new ZipOutputStream(new FileOutputStream(file));
+
+ // write manifest
+ out.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
+ try {
+ PrintWriter writer = new PrintWriter(new
OutputStreamWriter(out));
+ writer.println("Agent-Class: " + Agent.class.getName());
+ writer.println("Can-Redefine-Classes: true");
+ writer.println("Can-Retransform-Classes: true");
+ writer.flush();
+ } finally {
+ out.closeEntry();
+ }
+
+ // write agent class
+ out.putNextEntry(new ZipEntry(Agent.class.getName().replace('.',
'/') + ".class"));
+ try {
+ byte[] buffer = new byte[4096];
+ for (int count = in.read(buffer); count >= 0; count =
in.read(buffer)) {
+ out.write(buffer, 0, count);
+ }
+ } finally {
+ out.closeEntry();
+ }
+
+ return file.getAbsolutePath();
+ } catch (IOException e) {
+ if (file != null) {
+ file.delete();
+ }
+ throw e;
+ } finally {
+ close(in);
+ close(out);
+ }
+ }
+
+ private static void close(Closeable closeable) {
+ if (closeable != null) {
+ try {
+ closeable.close();
+ } catch (IOException ignored) {
+ }
+ }
+ }
}
Added:
openejb/trunk/openejb3/container/openejb-javaagent/src/test/java/org/apache/openejb/javaagent/AgentTest.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-javaagent/src/test/java/org/apache/openejb/javaagent/AgentTest.java?rev=581991&view=auto
==============================================================================
---
openejb/trunk/openejb3/container/openejb-javaagent/src/test/java/org/apache/openejb/javaagent/AgentTest.java
(added)
+++
openejb/trunk/openejb3/container/openejb-javaagent/src/test/java/org/apache/openejb/javaagent/AgentTest.java
Thu Oct 4 12:24:21 2007
@@ -0,0 +1,34 @@
+/**
+ *
+ * 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.javaagent;
+
+import junit.framework.TestCase;
+
+public class AgentTest extends TestCase {
+ public void test() {
+ try {
+ Class<?> vmClass =
Class.forName("com.sun.tools.attach.VirtualMachine");
+ vmClass.getMethod("attach", String.class);
+ vmClass.getMethod("loadAgent", String.class);
+ } catch (Exception e) {
+ // not a sun vm
+ return;
+ }
+ assertNotNull("Agent.getInstrumentation() is null",
Agent.getInstrumentation());
+ }
+}
Modified:
openejb/trunk/openejb3/container/openejb-loader/src/main/java/org/apache/openejb/loader/OpenEJBListener.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-loader/src/main/java/org/apache/openejb/loader/OpenEJBListener.java?rev=581991&r1=581990&r2=581991&view=diff
==============================================================================
---
openejb/trunk/openejb3/container/openejb-loader/src/main/java/org/apache/openejb/loader/OpenEJBListener.java
(original)
+++
openejb/trunk/openejb3/container/openejb-loader/src/main/java/org/apache/openejb/loader/OpenEJBListener.java
Thu Oct 4 12:24:21 2007
@@ -20,6 +20,7 @@
import org.apache.catalina.Container;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;
+import org.apache.catalina.ServerFactory;
import org.apache.catalina.Service;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.core.StandardHost;
@@ -29,20 +30,25 @@
import java.util.Properties;
public class OpenEJBListener implements LifecycleListener {
- static private boolean installed;
+ static private Boolean listenerInstalled;
-
- public static boolean isInstalled() {
- return installed;
+ public static boolean isListenerInstalled() {
+ new OpenEJBListener().tryDynamicInstall();
+ return listenerInstalled;
}
public OpenEJBListener() {
}
public void lifecycleEvent(LifecycleEvent event) {
+ // if installed hasn't been set yet, we can assume that
+ // this is the first entry into this code, meaning
+ // the listener was loaded from the server.xml and is
+ // installed into Tomcat.
+ if (listenerInstalled == null) listenerInstalled = true;
+
Object source = event.getSource();
if (source instanceof StandardServer) {
- installed = true;
StandardServer standardServer = (StandardServer) source;
init(standardServer);
}
@@ -137,5 +143,15 @@
}
}
return null;
+ }
+
+ private void tryDynamicInstall() {
+ // if installed hasn't been set yet, we can assume that
+ // this is the first entry into this code, meaning
+ // the listener was NOT installed into Tomcat
+ if (listenerInstalled == null) listenerInstalled = false;
+
+ StandardServer server = (StandardServer) ServerFactory.getServer();
+ this.init(server);
}
}