Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Wilkins2014-02-13 01:31:14 +0000
committerGreg Wilkins2014-02-13 01:31:14 +0000
commit18e19260ff2a0f99b7c935177287c77aa2c5588d (patch)
tree90a7a84a11880a876999ba038ee4156063050e3c /jetty-plus
parent1eb87fdddc1a7b68b9609b61b91ae5255dcba55b (diff)
downloadorg.eclipse.jetty.project-18e19260ff2a0f99b7c935177287c77aa2c5588d.tar.gz
org.eclipse.jetty.project-18e19260ff2a0f99b7c935177287c77aa2c5588d.tar.xz
org.eclipse.jetty.project-18e19260ff2a0f99b7c935177287c77aa2c5588d.zip
Apply changes resulting from the quickstart project
Diffstat (limited to 'jetty-plus')
-rw-r--r--jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/ContainerInitializer.java99
-rw-r--r--jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/LifeCycleCallbackCollection.java49
2 files changed, 143 insertions, 5 deletions
diff --git a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/ContainerInitializer.java b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/ContainerInitializer.java
index bff329e155..5edb826a69 100644
--- a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/ContainerInitializer.java
+++ b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/ContainerInitializer.java
@@ -18,15 +18,22 @@
package org.eclipse.jetty.plus.annotation;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import javax.servlet.ServletContainerInitializer;
import org.eclipse.jetty.util.ConcurrentHashSet;
import org.eclipse.jetty.util.Loader;
+import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.webapp.WebAppContext;
@@ -36,17 +43,41 @@ public class ContainerInitializer
private static final Logger LOG = Log.getLogger(ContainerInitializer.class);
final protected ServletContainerInitializer _target;
- final protected Class[] _interestedTypes;
- protected Set<String> _applicableTypeNames = new ConcurrentHashSet<String>();
- protected Set<String> _annotatedTypeNames = new ConcurrentHashSet<String>();
+ final protected Class<?>[] _interestedTypes;
+ final protected Set<String> _applicableTypeNames = new ConcurrentHashSet<String>();
+ final protected Set<String> _annotatedTypeNames = new ConcurrentHashSet<String>();
- public ContainerInitializer (ServletContainerInitializer target, Class[] classes)
+ public ContainerInitializer (ServletContainerInitializer target, Class<?>[] classes)
{
_target = target;
_interestedTypes = classes;
}
-
+
+ public ContainerInitializer (ClassLoader loader, String toString)
+ {
+ Matcher m = Pattern.compile("ContainerInitializer\\{(.*),interested=(.*),applicable=(.*),annotated=(.*)\\}").matcher(toString);
+ if (!m.matches())
+ throw new IllegalArgumentException(toString);
+
+ try
+ {
+ _target = (ServletContainerInitializer)loader.loadClass(m.group(1)).newInstance();
+ String[] interested = StringUtil.arrayFromString(m.group(2));
+ _interestedTypes = new Class<?>[interested.length];
+ for (int i=0;i<interested.length;i++)
+ _interestedTypes[i]=loader.loadClass(interested[i]);
+ for (String s:StringUtil.arrayFromString(m.group(3)))
+ _applicableTypeNames.add(s);
+ for (String s:StringUtil.arrayFromString(m.group(4)))
+ _annotatedTypeNames.add(s);
+ }
+ catch(Exception e)
+ {
+ throw new IllegalArgumentException(toString, e);
+ }
+ }
+
public ServletContainerInitializer getTarget ()
{
return _target;
@@ -116,4 +147,62 @@ public class ContainerInitializer
}
}
}
+
+ public String toString()
+ {
+ List<String> interested = new ArrayList<>(_interestedTypes.length);
+ for (Class<?> c : _interestedTypes)
+ interested.add(c.getName());
+ return String.format("ContainerInitializer{%s,interested=%s,applicable=%s,annotated=%s}",_target.getClass().getName(),interested,_applicableTypeNames,_annotatedTypeNames);
+ }
+
+ public void resolveClasses(WebAppContext context, Map<String, Set<String>> classMap)
+ {
+ //We have already found the classes that directly have an annotation that was in the HandlesTypes
+ //annotation of the ServletContainerInitializer. For each of those classes, walk the inheritance
+ //hierarchy to find classes that extend or implement them.
+ Set<String> annotatedClassNames = getAnnotatedTypeNames();
+ if (annotatedClassNames != null && !annotatedClassNames.isEmpty())
+ {
+ for (String name : annotatedClassNames)
+ {
+ //add the class that has the annotation
+ addApplicableTypeName(name);
+
+ //find and add the classes that inherit the annotation
+ addInheritedTypes(classMap, (Set<String>)classMap.get(name));
+ }
+ }
+
+
+ //Now we need to look at the HandlesTypes classes that were not annotations. We need to
+ //find all classes that extend or implement them.
+ if (getInterestedTypes() != null)
+ {
+ for (Class<?> c : getInterestedTypes())
+ {
+ if (!c.isAnnotation())
+ {
+ //find and add the classes that implement or extend the class.
+ //but not including the class itself
+ addInheritedTypes(classMap, (Set<String>)classMap.get(c.getName()));
+ }
+ }
+ }
+ }
+
+ private void addInheritedTypes(Map<String, Set<String>> classMap,Set<String> names)
+ {
+ if (names == null || names.isEmpty())
+ return;
+
+ for (String s : names)
+ {
+ //add the name of the class
+ addApplicableTypeName(s);
+
+ //walk the hierarchy and find all types that extend or implement the class
+ addInheritedTypes(classMap, (Set<String>)classMap.get(s));
+ }
+ }
}
diff --git a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/LifeCycleCallbackCollection.java b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/LifeCycleCallbackCollection.java
index 2ebd8a49de..27b88fe100 100644
--- a/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/LifeCycleCallbackCollection.java
+++ b/jetty-plus/src/main/java/org/eclipse/jetty/plus/annotation/LifeCycleCallbackCollection.java
@@ -19,6 +19,8 @@
package org.eclipse.jetty.plus.annotation;
import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -136,4 +138,51 @@ public class LifeCycleCallbackCollection
for (int i=0;i<callbacks.size();i++)
((LifeCycleCallback)callbacks.get(i)).callback(o);
}
+
+ /**
+ * Generate a read-only view of the post-construct callbacks
+ * @return
+ */
+ public Map<String, List<LifeCycleCallback>> getPostConstructCallbackMap()
+ {
+ return Collections.unmodifiableMap(postConstructCallbacksMap);
+ }
+
+ /**
+ * Generate a read-only view of the pre-destroy callbacks
+ * @return
+ */
+ public Map<String, List<LifeCycleCallback>> getPreDestroyCallbackMap()
+ {
+ return Collections.unmodifiableMap(preDestroyCallbacksMap);
+ }
+
+ /**
+ * Amalgamate all post-construct callbacks and return a read only list
+ * @return
+ */
+ public Collection<LifeCycleCallback> getPostConstructCallbacks()
+ {
+ List<LifeCycleCallback> list = new ArrayList<LifeCycleCallback>();
+ for (String s:postConstructCallbacksMap.keySet())
+ {
+ list.addAll(postConstructCallbacksMap.get(s));
+ }
+ return Collections.unmodifiableCollection(list);
+ }
+
+ /**
+ * Amalgamate all pre-destroy callbacks and return a read only list
+ * @return
+ */
+ public Collection<LifeCycleCallback> getPreDestroyCallbacks()
+ {
+ List<LifeCycleCallback> list = new ArrayList<LifeCycleCallback>();
+ for (String s:preDestroyCallbacksMap.keySet())
+ {
+ list.addAll(preDestroyCallbacksMap.get(s));
+ }
+ return Collections.unmodifiableCollection(list);
+ }
+
}

Back to the top