View Javadoc

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