Hi all.

        I wrote some code to assign an unique identifier to the running Turbine 
instance.
It can be used for prefixing names of files in common temporary directory on a server,
or to identify user's preferences for this particular Turbine application on an LDAP
sever and possibly many other purproses.

        My idea for generating an unique id was taking full servlet URL (schema,
host, port, context & servlet name) and calculating an MD5 digest of it. The input
data is unique both on the server and on the network where the application is visible
from. Of course there's still 1:2^128 probability that there will be an id clash, but
it's a nincredibly small chance, and the resulting ids are well defined, one-way and
pretty.

I'm ready to check the following patch in. If no one objects I'll do it on Monday.

Rafal

-------------------------------------------------------------------------------------
diff -u -r1.3 Turbine.java
--- Turbine.java        2000/08/09 19:30:53     1.3
+++ Turbine.java        2000/08/18 10:30:14
@@ -199,6 +199,8 @@
                 {
                     if(firstDoGet) 
                     {
+                        // generate unique indetifier of this Turbine instance
+                        TurbineResources.generateId(data);
                         // ceratin services need RunData to initialize themselves
                         TurbineServices.getInstance().initServices(data);
                         // mark that we're done
diff -u -r1.3 TurbineResources.java
--- org/apache/turbine/services/resources/TurbineResources.java 2000/08/11 20:23:55    
 1.3
+++ org/apache/turbine/services/resources/TurbineResources.java 2000/08/18 10:30:14
@@ -61,6 +61,12 @@
 import org.apache.java.util.Configurations;
 import org.apache.java.util.ExtendedProperties;
 
+import org.apache.turbine.util.RunData;
+import javax.servlet.http.HttpServletRequest;  
+import javax.servlet.http.HttpUtils;
+import org.apache.java.security.MD5;
+import org.apache.java.lang.Bytes;
+
 /**
  * This is a mostly static class for accessing the TurbineResources.properties
  * file. It is built in such a way that it is also possible to access this class
@@ -108,6 +114,7 @@
     public static final String LOGIN_MESSAGE_NOSCREEN = "login.message.noscreen";
     public static final String LOGOUT_MESSAGE = "logout.message";
     public static final String DB_IDBROKER_CLEVERQUANTITY = 
"database.idbroker.cleverquantity";
+    public static final String INSTANCE_ID_KEY = "turbine.instance.id";
     
     /** The name of the file to load properties from. */
     private static String fileName = null;
@@ -457,4 +464,35 @@
             return def;
         return vec;
     }
+    
+    /**
+     * This method allows modification of configuration resources at runtime. It is 
+declared
+     * as package private to prevent unintetional or malicious breaking the 
+configuration.
+     *
+     * @param name The resource name
+     * @param value The value that the resource should be assigned (will be converted 
+into a String)
+     */
+    static void setResource(String name, Object value)  {
+        getGeneric().getConfig().getRepository().put(name, value.toString());
+    }
+    
+    /**
+     * <p> This method generates unique identifier of this Turbine instance upon the 
+first request.
+     * It can be retrieved from TurbineResources under the key 
+<code>turbine.instance.id</code>
+     *  
+     * <p> The identifier is an MD5 digest of a  string comprised of schema, host 
+addres, port number,
+     * context path and servlet name of this Turbine instance. Wiht overwhelming 
+probability, this is 
+     * unique for the host where the server is running, but also for the whole realm 
+where your Turbine 
+     * application is accesible from, be it your intranet or the Internet.
+     */
+    public static void generateId( RunData data )  {
+        String url = HttpUtils.getRequestURL(data.getRequest()).toString();
+        MD5 md5 = new MD5();
+        String turbineId = Bytes.toString(md5.digest(url.getBytes()));
+        
+        setResource(TurbineResources.INSTANCE_ID_KEY, turbineId);
+
+        org.apache.turbine.util.Log.note("This Turbine instance 
+#"+getString(TurbineResources.INSTANCE_ID_KEY));
+    }
+    
 }


------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Search: <http://www.mail-archive.com/turbine%40list.working-dogs.com/>
Problems?:           [EMAIL PROTECTED]

Reply via email to