View Javadoc

1   package org.apache.turbine.services.uniqueid;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.security.MessageDigest;
20  
21  import org.apache.commons.codec.binary.Base64;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  import org.apache.turbine.Turbine;
27  import org.apache.turbine.services.InitializationException;
28  import org.apache.turbine.services.TurbineBaseService;
29  import org.apache.turbine.util.GenerateUniqueId;
30  import org.apache.turbine.util.RunData;
31  
32  /***
33   * <p> This is an implementation of {@link UniqueIdService}.
34   *
35   * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
36   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37   * @version $Id: TurbineUniqueIdService.java 264148 2005-08-29 14:21:04Z henning $
38   */
39  public class TurbineUniqueIdService
40          extends TurbineBaseService
41          implements UniqueIdService
42  {
43      /*** Logging */
44      private static Log log = LogFactory.getLog(TurbineUniqueIdService.class);
45  
46      /*** The identifier of this instance of turbine. */
47      protected static String turbineId = "UNKNOWN";
48  
49      protected static String turbineURL = "UNKNOWN";
50  
51      protected static int counter;
52  
53      /***
54       * @deprecated Use init() instead
55       */
56      public void init(RunData data)
57              throws InitializationException
58      {
59          init();
60      }
61  
62      /***
63       * <p> Initializes the service upon first Turbine.doGet()
64       * invocation.
65       */
66      public void init()
67              throws InitializationException
68      {
69          try
70          {
71              // This might be a problem if the unique Id Service runs
72              // before Turbine got its first request. In this case,
73              // getDefaultServerData will return just a dummy value
74              // which is the same for all instances of Turbine.
75              //
76              // @todo This needs definitely further working.
77              String url = Turbine.getDefaultServerData().toString();
78  
79              MessageDigest md = MessageDigest.getInstance("MD5");
80              byte [] bytesId = md.digest(url.getBytes("UTF-8"));
81              turbineId = new String(Base64.encodeBase64(bytesId));
82  
83              log.info("This is Turbine instance running at: " + url);
84              log.info("The instance id is #" + turbineId);
85              setInit(true);
86          }
87          catch (Exception e)
88          {
89              throw new InitializationException(
90                      "Could not initialize TurbineUniqueId Service", e);
91          }
92      }
93  
94      /***
95       * <p> Writes a message to the log upon system shutdown.
96       */
97      public void shutdown()
98      {
99          log.info("Turbine instance running at " + turbineURL + " shutting down.");
100     }
101 
102     /***
103      * <p> Returns an identifier of this Turbine instance that is unique
104      * both on the server and worldwide.  This identifier is computed
105      * as an MD5 sum of the URL (including schema, address, port if
106      * different that 80/443 respecively, context and servlet name).
107      * There is an overwhelming probalility that this id will be
108      * different that all other Turbine instances online.
109      *
110      * @return A String with the instance identifier.
111      */
112     public String getInstanceId()
113     {
114         return turbineId;
115     }
116 
117     /***
118      * <p> Returns an identifier that is unique within this turbine
119      * instance, but does not have random-like apearance.
120      *
121      * @return A String with the non-random looking instance
122      * identifier.
123      */
124     public String getUniqueId()
125     {
126         int current;
127         synchronized (TurbineUniqueIdService.class)
128         {
129             current = counter++;
130         }
131         String id = Integer.toString(current);
132 
133         // If you manage to get more than 100 million of ids, you'll
134         // start getting ids longer than 8 characters.
135         if (current < 100000000)
136         {
137             id = ("00000000" + id).substring(id.length());
138         }
139         return id;
140     }
141 
142     /***
143      * <p> Returns a unique identifier that looks like random data.
144      *
145      * @return A String with the random looking instance identifier.
146      */
147     public String getPseudorandomId()
148     {
149         return GenerateUniqueId.getIdentifier();
150     }
151 }