You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by at...@apache.org on 2008/09/03 17:45:49 UTC

svn commit: r691639 - /portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/LifecycleAwareStaticClassInitializer.java

Author: ate
Date: Wed Sep  3 08:45:49 2008
New Revision: 691639

URL: http://svn.apache.org/viewvc?rev=691639&view=rev
Log:
Externalizing injection of Spring provided (manager) bean references which are needed by each and every instance of a certain class.
The LifecycleAwareStaticClassInitializer can automatically inject the provided bean on Spring context initialization time and remove it again when the Spring context is destroyed using its init() and destroy() method.

Added:
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/LifecycleAwareStaticClassInitializer.java   (with props)

Added: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/LifecycleAwareStaticClassInitializer.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/LifecycleAwareStaticClassInitializer.java?rev=691639&view=auto
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/LifecycleAwareStaticClassInitializer.java (added)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/LifecycleAwareStaticClassInitializer.java Wed Sep  3 08:45:49 2008
@@ -0,0 +1,80 @@
+/*
+ * 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.jetspeed.components;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+/**
+ * Utility bean which can be used to initialize a static class member with a Spring managed (singleton) bean on context initialization and removing it
+ * automatically again when the context is destroyed.
+ * 
+ * The target class needs a public static (void) method with one single object argument. This bean needs to be configured with the target class name,
+ * the target static method name, the class name of the target value and finally the value (e.g. a bean reference) itself.
+ * 
+ * The value will be "injected" into the target class when the init() method is called.
+ * When the destroy() method is called, the same method will be called again with a null value.
+ * @version $Id$
+ *
+ */
+public class LifecycleAwareStaticClassInitializer
+{
+    private String className;
+    private String methodName;
+    private String typeName;
+    private Object value;
+    private Method method;
+    
+    public void setClassName(String className)
+    {
+        this.className = className;
+    }
+
+    public void setMethodName(String methodName)
+    {
+        this.methodName = methodName;
+    }
+    
+    public void setTypeName(String typeName)
+    {
+        this.typeName = typeName;
+    }
+
+    public void setValue(Object value)
+    {
+        this.value = value;
+    }
+    
+    @SuppressWarnings("unchecked")
+    public void init() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
+    {
+        Class clazz = Class.forName(className);
+        Class type = Class.forName(typeName);
+        Method method = clazz.getMethod(methodName, type);
+        method.invoke(null, value);
+        this.method = method;
+    }
+    
+    public void destroy() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
+    {
+        if (method != null)
+        {
+            method.invoke(null, (Object)null);
+        }
+    }
+}

Propchange: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/LifecycleAwareStaticClassInitializer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/LifecycleAwareStaticClassInitializer.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-cm/src/main/java/org/apache/jetspeed/components/LifecycleAwareStaticClassInitializer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org