View Javadoc

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