Revision: 14956
Author: [email protected]
Date: Mon Nov 1 14:59:41 2010
Log: [No log message]
http://code.google.com/p/mobicents/source/detail?r=14956
Added:
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents/servlet/sip/annotations/SipInstanceManager.java
/branches/servers/sip-servlets/2.0/tomcat-7/target
Deleted:
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents/servlet/sip/annotations/SipAnnotationProcessor.java
Modified:
/branches/servers/sip-servlets/2.0/tomcat-7
/branches/servers/sip-servlets/2.0/tomcat-7/lib
/branches/servers/sip-servlets/2.0/tomcat-7/src
/branches/servers/sip-servlets/2.0/tomcat-7/src/main
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/jboss
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/jboss/web
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/jboss/web/tomcat
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/jboss/web/tomcat/service
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/jboss/web/tomcat/service/session
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents/servlet
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents/servlet/sip
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents/servlet/sip/annotations
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents/servlet/sip/startup
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents/servlet/sip/startup/SipNamingContextListener.java
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents/servlet/sip/startup/SipStandardContext.java
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents/servlet/sip/startup/jboss
=======================================
--- /dev/null
+++
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents/servlet/sip/annotations/SipInstanceManager.java
Mon Nov 1 14:59:41 2010
@@ -0,0 +1,166 @@
+/*
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.mobicents.servlet.sip.annotations;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Map;
+
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceUnit;
+import javax.xml.ws.WebServiceRef;
+
+import org.apache.catalina.core.DefaultInstanceManager;
+import org.mobicents.servlet.sip.startup.SipContext;
+/**
+ * An annotation processor which tries to inject resources found in the
servlet context
+ * instead of the naming context of the servlet.
+ *
+ * @author Vladimir Ralev
+ *
+ */
+public class SipInstanceManager extends DefaultInstanceManager {
+
+ private SipContext sipContext;
+ private Context context;
+
+ @Override
+ public void processAnnotations(Object instance, Map<String, String>
injections) throws IllegalAccessException, InvocationTargetException,
NamingException {
+ if (context == null) {
+ // No resource injection
+ return;
+ }
+
+ // Initialize fields annotations
+ Field[] fields = instance.getClass().getDeclaredFields();
+ for (int i = 0; i < fields.length; i++) {
+ if (fields[i].isAnnotationPresent(Resource.class)) {
+ Resource annotation = (Resource)
fields[i].getAnnotation(Resource.class);
+ if(!lookupResourceInServletContext(instance, fields[i],
annotation.name()))
+ lookupFieldResource(context, instance, fields[i],
annotation.name());
+ }
+ if (fields[i].isAnnotationPresent(EJB.class)) {
+ EJB annotation = (EJB)
fields[i].getAnnotation(EJB.class);
+ lookupFieldResource(context, instance, fields[i],
annotation.name());
+ }
+ if (fields[i].isAnnotationPresent(WebServiceRef.class)) {
+ WebServiceRef annotation =
+ (WebServiceRef)
fields[i].getAnnotation(WebServiceRef.class);
+ lookupFieldResource(context, instance, fields[i],
annotation.name());
+ }
+ if
(fields[i].isAnnotationPresent(PersistenceContext.class)) {
+ PersistenceContext annotation =
+ (PersistenceContext)
fields[i].getAnnotation(PersistenceContext.class);
+ lookupFieldResource(context, instance, fields[i],
annotation.name());
+ }
+ if (fields[i].isAnnotationPresent(PersistenceUnit.class)) {
+ PersistenceUnit annotation =
+ (PersistenceUnit)
fields[i].getAnnotation(PersistenceUnit.class);
+ lookupFieldResource(context, instance, fields[i],
annotation.name());
+ }
+ }
+ }
+
+ protected boolean lookupResourceInServletContext(Object instance, Field
field, String annotationName) {
+ String typeName = field.getType().getCanonicalName();
+ if(annotationName == null || annotationName.equals("")) annotationName =
typeName;
+ Object objectToInject =
sipContext.getServletContext().getAttribute(annotationName);
+ if(objectToInject != null &&
+
field.getType().isAssignableFrom(objectToInject.getClass())) {
+ boolean accessibility = false;
+ accessibility = field.isAccessible();
+ field.setAccessible(true);
+ try {
+ field.set(instance, objectToInject);
+ } catch (IllegalArgumentException e) {
+ throw e;
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+ field.setAccessible(accessibility);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Inject resources in specified field.
+ */
+ protected static void lookupFieldResource(javax.naming.Context context,
+ Object instance, Field field, String name)
+ throws NamingException, IllegalAccessException {
+
+ Object lookedupResource = null;
+ boolean accessibility = false;
+
+ if ((name != null) &&
+ (name.length() > 0)) {
+ lookedupResource = context.lookup(name);
+ } else {
+ if(field.getClass().getName().startsWith("javax.servlet.sip")) {
+ lookedupResource = context.lookup("sip/" +
instance.getClass().getName() + "/" + field.getName());
+ } else {
+ lookedupResource = context.lookup(instance.getClass().getName()
+ "/" + field.getName());
+ }
+ }
+
+ accessibility = field.isAccessible();
+ field.setAccessible(true);
+ field.set(instance, lookedupResource);
+ field.setAccessible(accessibility);
+ }
+
+ public SipInstanceManager(Context context, Map<String, Map<String,
String>> injectionMap, org.apache.catalina.Context catalinaContext,
ClassLoader containerClassLoader) {
+ super(context, injectionMap, catalinaContext,
containerClassLoader);
+ this.context = context;
+ this.sipContext = sipContext;
+ }
+
+ /**
+ * @param sipContext the sipContext to set
+ */
+ public void setSipContext(SipContext sipContext) {
+ this.sipContext = sipContext;
+ }
+
+ /**
+ * @return the sipContext
+ */
+ public SipContext getSipContext() {
+ return sipContext;
+ }
+
+ /**
+ * @param context the context to set
+ */
+ public void setContext(Context context) {
+ this.context = context;
+ }
+
+ /**
+ * @return the context
+ */
+ public Context getContext() {
+ return context;
+ }
+
+}
=======================================
---
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents/servlet/sip/annotations/SipAnnotationProcessor.java
Fri Jan 16 07:01:49 2009
+++ /dev/null
@@ -1,162 +0,0 @@
-/*
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.mobicents.servlet.sip.annotations;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
-
-import javax.annotation.Resource;
-import javax.ejb.EJB;
-import javax.naming.Context;
-import javax.naming.NamingException;
-import javax.persistence.PersistenceContext;
-import javax.persistence.PersistenceUnit;
-import javax.xml.ws.WebServiceRef;
-
-import org.apache.catalina.util.DefaultAnnotationProcessor;
-import org.mobicents.servlet.sip.startup.SipContext;
-/**
- * An annotation processor which tries to inject resources found in the
servlet context
- * instead of the naming context of the servlet.
- *
- * @author Vladimir Ralev
- *
- */
-public class SipAnnotationProcessor extends DefaultAnnotationProcessor {
-
- private SipContext sipContext;
-
- @Override
- public void processAnnotations(Object instance) throws
IllegalAccessException, InvocationTargetException, NamingException {
- if (context == null) {
- // No resource injection
- return;
- }
-
- // Initialize fields annotations
- Field[] fields = instance.getClass().getDeclaredFields();
- for (int i = 0; i < fields.length; i++) {
- if (fields[i].isAnnotationPresent(Resource.class)) {
- Resource annotation = (Resource)
fields[i].getAnnotation(Resource.class);
- if(!lookupResourceInServletContext(instance, fields[i],
annotation.name()))
- lookupFieldResource(context, instance, fields[i],
annotation.name());
- }
- if (fields[i].isAnnotationPresent(EJB.class)) {
- EJB annotation = (EJB)
fields[i].getAnnotation(EJB.class);
- lookupFieldResource(context, instance, fields[i],
annotation.name());
- }
- if (fields[i].isAnnotationPresent(WebServiceRef.class)) {
- WebServiceRef annotation =
- (WebServiceRef)
fields[i].getAnnotation(WebServiceRef.class);
- lookupFieldResource(context, instance, fields[i],
annotation.name());
- }
- if
(fields[i].isAnnotationPresent(PersistenceContext.class)) {
- PersistenceContext annotation =
- (PersistenceContext)
fields[i].getAnnotation(PersistenceContext.class);
- lookupFieldResource(context, instance, fields[i],
annotation.name());
- }
- if (fields[i].isAnnotationPresent(PersistenceUnit.class)) {
- PersistenceUnit annotation =
- (PersistenceUnit)
fields[i].getAnnotation(PersistenceUnit.class);
- lookupFieldResource(context, instance, fields[i],
annotation.name());
- }
- }
- }
-
- protected boolean lookupResourceInServletContext(Object instance, Field
field, String annotationName) {
- String typeName = field.getType().getCanonicalName();
- if(annotationName == null || annotationName.equals("")) annotationName =
typeName;
- Object objectToInject =
sipContext.getServletContext().getAttribute(annotationName);
- if(objectToInject != null &&
-
field.getType().isAssignableFrom(objectToInject.getClass())) {
- boolean accessibility = false;
- accessibility = field.isAccessible();
- field.setAccessible(true);
- try {
- field.set(instance, objectToInject);
- } catch (IllegalArgumentException e) {
- throw e;
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- }
- field.setAccessible(accessibility);
- return true;
- }
- return false;
- }
-
- /**
- * Inject resources in specified field.
- */
- protected static void lookupFieldResource(javax.naming.Context context,
- Object instance, Field field, String name)
- throws NamingException, IllegalAccessException {
-
- Object lookedupResource = null;
- boolean accessibility = false;
-
- if ((name != null) &&
- (name.length() > 0)) {
- lookedupResource = context.lookup(name);
- } else {
- if(field.getClass().getName().startsWith("javax.servlet.sip")) {
- lookedupResource = context.lookup("sip/" +
instance.getClass().getName() + "/" + field.getName());
- } else {
- lookedupResource = context.lookup(instance.getClass().getName()
+ "/" + field.getName());
- }
- }
-
- accessibility = field.isAccessible();
- field.setAccessible(true);
- field.set(instance, lookedupResource);
- field.setAccessible(accessibility);
- }
-
- public SipAnnotationProcessor(Context context, SipContext sipContext) {
- super(context);
- this.sipContext = sipContext;
- }
-
- /**
- * @param sipContext the sipContext to set
- */
- public void setSipContext(SipContext sipContext) {
- this.sipContext = sipContext;
- }
-
- /**
- * @return the sipContext
- */
- public SipContext getSipContext() {
- return sipContext;
- }
-
- /**
- * @param context the context to set
- */
- public void setContext(Context context) {
- this.context = context;
- }
-
- /**
- * @return the context
- */
- public Context getContext() {
- return context;
- }
-
-}
=======================================
---
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents/servlet/sip/startup/SipNamingContextListener.java
Tue Sep 15 04:05:45 2009
+++
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents/servlet/sip/startup/SipNamingContextListener.java
Mon Nov 1 14:59:41 2010
@@ -28,7 +28,7 @@
import org.apache.catalina.core.NamingContextListener;
import org.apache.log4j.Logger;
import org.apache.naming.ContextAccessController;
-import org.mobicents.servlet.sip.annotations.SipAnnotationProcessor;
+import org.mobicents.servlet.sip.annotations.SipInstanceManager;
/**
* Helper class used to initialize and populate the JNDI context associated
@@ -60,7 +60,7 @@
super.lifecycleEvent(event);
if (event.getType().equalsIgnoreCase(Lifecycle.START_EVENT)) {
if (container instanceof SipContext) {
-
((SipAnnotationProcessor)((SipContext)container).getAnnotationProcessor()).setContext(envCtx);
+
((SipInstanceManager)((SipContext)container).getAnnotationProcessor()).setContext(envCtx);
}
}
}
=======================================
---
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents/servlet/sip/startup/SipStandardContext.java
Tue Oct 12 11:50:44 2010
+++
/branches/servers/sip-servlets/2.0/tomcat-7/src/main/java/org/mobicents/servlet/sip/startup/SipStandardContext.java
Mon Nov 1 14:59:41 2010
@@ -61,7 +61,7 @@
import org.jboss.web.tomcat.service.session.SnapshotSipManager;
import org.mobicents.servlet.sip.SipConnector;
import org.mobicents.servlet.sip.annotation.ConcurrencyControlMode;
-import org.mobicents.servlet.sip.annotations.SipAnnotationProcessor;
+import org.mobicents.servlet.sip.annotations.SipInstanceManager;
import org.mobicents.servlet.sip.core.SipApplicationDispatcher;
import org.mobicents.servlet.sip.core.SipContextEvent;
import org.mobicents.servlet.sip.core.SipContextEventType;
@@ -179,7 +179,7 @@
}
@Override
- public void init() throws Exception {
+ public void initInternal() throws LifecycleException { // Gvag changed
from init to initInternal
if(logger.isInfoEnabled()) {
logger.info("Initializing the sip context");
}
@@ -312,7 +312,7 @@
// normal lookup method - in the default naming context.
//tomcat naming
this.setAnnotationProcessor(
- new SipAnnotationProcessor(
+ new SipInstanceManager(
getNamingContextListener().getEnvContext(),
this));
} else {
@@ -321,7 +321,7 @@
InitialContext iniCtx = new InitialContext();
Context envCtx = (Context)
iniCtx.lookup("java:comp/env");
this.setAnnotationProcessor(
- new SipAnnotationProcessor(
+ new SipInstanceManager(
envCtx,
this));
} catch (NamingException e) {
@@ -339,11 +339,11 @@
// for SipFactory, Session utils and other objects residing in the
servlet context space.
// Of course if the variable is not found in in the servet context it
defaults to the
// normal lookup method - in the default naming context.
- if(getAnnotationProcessor() == null || !(getAnnotationProcessor()
instanceof SipAnnotationProcessor)) {
+ if(getInstanceManager() == null || !(getInstanceManager() instanceof
SipInstanceManager)) {
if(isUseNaming()) {
//tomcat naming
- this.setAnnotationProcessor(
- new
SipAnnotationProcessor(
+ this.setInstanceManager(
+ new SipInstanceManager(
getNamingContextListener().getEnvContext(),
this));
} else {
@@ -352,7 +352,7 @@
InitialContext iniCtx = new
InitialContext();
Context envCtx = (Context)
iniCtx.lookup("java:comp/env");
this.setAnnotationProcessor(
- new
SipAnnotationProcessor(
+ new
SipInstanceManager(
envCtx,
this));
} catch (NamingException e) {
@@ -597,14 +597,14 @@
}
protected void loadSipJNDIContext() {
- if(getAnnotationProcessor() instanceof SipAnnotationProcessor) {
+ if(getAnnotationProcessor() instanceof SipInstanceManager) {
if(getNamingContextListener() != null) {
-
((SipAnnotationProcessor)getAnnotationProcessor()).setContext(getNamingContextListener().getEnvContext());
+
((SipInstanceManager)getAnnotationProcessor()).setContext(getNamingContextListener().getEnvContext());
} else {
try {
InitialContext iniCtx = new
InitialContext();
Context envCtx = (Context)
iniCtx.lookup("java:comp/env");
-
((SipAnnotationProcessor)getAnnotationProcessor()).setContext(envCtx);
+
((SipInstanceManager)getAnnotationProcessor()).setContext(envCtx);
} catch (NamingException e) {
logger.error("Impossible to get the naming
context ", e);
throw new IllegalStateException(e);