001: package org.apache.turbine.services.uniqueid;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.security.MessageDigest;
023:
024: import org.apache.commons.codec.binary.Base64;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: import org.apache.turbine.Turbine;
030: import org.apache.turbine.services.InitializationException;
031: import org.apache.turbine.services.TurbineBaseService;
032: import org.apache.turbine.util.GenerateUniqueId;
033: import org.apache.turbine.util.RunData;
034:
035: /**
036: * <p> This is an implementation of {@link UniqueIdService}.
037: *
038: * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
039: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
040: * @version $Id: TurbineUniqueIdService.java 534527 2007-05-02 16:10:59Z tv $
041: */
042: public class TurbineUniqueIdService extends TurbineBaseService
043: implements UniqueIdService {
044: /** Logging */
045: private static Log log = LogFactory
046: .getLog(TurbineUniqueIdService.class);
047:
048: /** The identifier of this instance of turbine. */
049: protected static String turbineId = "UNKNOWN";
050:
051: protected static String turbineURL = "UNKNOWN";
052:
053: protected static int counter;
054:
055: /**
056: * @deprecated Use init() instead
057: */
058: public void init(RunData data) throws InitializationException {
059: init();
060: }
061:
062: /**
063: * <p> Initializes the service upon first Turbine.doGet()
064: * invocation.
065: */
066: public void init() throws InitializationException {
067: try {
068: // This might be a problem if the unique Id Service runs
069: // before Turbine got its first request. In this case,
070: // getDefaultServerData will return just a dummy value
071: // which is the same for all instances of Turbine.
072: //
073: // @todo This needs definitely further working.
074: String url = Turbine.getDefaultServerData().toString();
075:
076: MessageDigest md = MessageDigest.getInstance("MD5");
077: byte[] bytesId = md.digest(url.getBytes("UTF-8"));
078: turbineId = new String(Base64.encodeBase64(bytesId));
079:
080: log.info("This is Turbine instance running at: " + url);
081: log.info("The instance id is #" + turbineId);
082: setInit(true);
083: } catch (Exception e) {
084: throw new InitializationException(
085: "Could not initialize TurbineUniqueId Service", e);
086: }
087: }
088:
089: /**
090: * <p> Writes a message to the log upon system shutdown.
091: */
092: public void shutdown() {
093: log.info("Turbine instance running at " + turbineURL
094: + " shutting down.");
095: }
096:
097: /**
098: * <p> Returns an identifier of this Turbine instance that is unique
099: * both on the server and worldwide. This identifier is computed
100: * as an MD5 sum of the URL (including schema, address, port if
101: * different that 80/443 respecively, context and servlet name).
102: * There is an overwhelming probalility that this id will be
103: * different that all other Turbine instances online.
104: *
105: * @return A String with the instance identifier.
106: */
107: public String getInstanceId() {
108: return turbineId;
109: }
110:
111: /**
112: * <p> Returns an identifier that is unique within this turbine
113: * instance, but does not have random-like apearance.
114: *
115: * @return A String with the non-random looking instance
116: * identifier.
117: */
118: public String getUniqueId() {
119: int current;
120: synchronized (TurbineUniqueIdService.class) {
121: current = counter++;
122: }
123: String id = Integer.toString(current);
124:
125: // If you manage to get more than 100 million of ids, you'll
126: // start getting ids longer than 8 characters.
127: if (current < 100000000) {
128: id = ("00000000" + id).substring(id.length());
129: }
130: return id;
131: }
132:
133: /**
134: * <p> Returns a unique identifier that looks like random data.
135: *
136: * @return A String with the random looking instance identifier.
137: */
138: public String getPseudorandomId() {
139: return GenerateUniqueId.getIdentifier();
140: }
141: }
|