View Javadoc
1 package org.apache.turbine.services.uniqueid; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import org.apache.turbine.util.Log; 58 import org.apache.turbine.util.RunData; 59 import org.apache.turbine.util.GenerateUniqueId; 60 import org.apache.turbine.services.TurbineBaseService; 61 import org.apache.java.security.MD5; 62 import org.apache.java.lang.Bytes; 63 64 /*** 65 * <p> This is an implementation of {@link UniqueIdService}. 66 * 67 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a> 68 * @version $Id: TurbineUniqueIdService.java,v 1.1.1.1 2001/08/16 05:09:23 jvanzyl Exp $ 69 */ 70 public class TurbineUniqueIdService 71 extends TurbineBaseService 72 implements UniqueIdService 73 { 74 /*** The identifier of this instance of turbine. */ 75 protected static String turbineId = "UNKNOWN"; 76 77 protected static String turbineURL = "UNKNOWN"; 78 79 protected static int counter; 80 81 /*** 82 * <p> Initializes the service upon first Turbine.doGet() 83 * invocation. 84 * 85 * @param data A Turbine RunData object. 86 */ 87 public void init( RunData data ) 88 { 89 StringBuffer url = new StringBuffer(); 90 url.append ( data.getServerScheme() ); 91 url.append ( "://" ); 92 url.append ( data.getServerName() ); 93 if ( (data.getServerScheme().equals("http") && 94 data.getServerPort() != 80) || 95 (data.getServerScheme().equals("https") && 96 data.getServerPort() != 443) 97 ) 98 { 99 url.append (":"); 100 url.append ( data.getServerPort() ); 101 } 102 103 url.append ( data.getServerData().getScriptName() ); 104 turbineURL = url.toString(); 105 106 MD5 md5 = new MD5(); 107 turbineId = Bytes.toString(md5.digest(url.toString().getBytes())); 108 109 Log.info("This is Turbine instance running at: " + url); 110 Log.info("The instance id is #" + turbineId); 111 setInit(true); 112 } 113 114 /*** 115 * <p> Writes a message to the log upon system shutdown. 116 */ 117 public void shutdown() 118 { 119 Log.info("Turbine instance running at " + turbineURL + " shutting down."); 120 } 121 122 /*** 123 * <p> Returs an identifer of this Turbine instance that is unique 124 * both on the server and worldwide. This identifier is computed 125 * as an MD5 sum of the URL (including schema, addres, port if 126 * different that 80/443 respecively, context and servlet name). 127 * There is an overwhelming probalility that this id will be 128 * different that all other Turbine instances online. 129 * 130 * @return A String with the instance identifier. 131 */ 132 public String getInstanceId() 133 { 134 return turbineId; 135 } 136 137 /*** 138 * <p> Returns an identifier that is unique within this turbine 139 * instance, but does not have random-like apearance. 140 * 141 * @return A String with the non-random looking instance 142 * identifier. 143 */ 144 public String getUniqueId() 145 { 146 int current; 147 synchronized(TurbineUniqueIdService.class) 148 { 149 current = counter++; 150 } 151 String id = Integer.toString(current); 152 153 // If you manage to get more than 100 million of ids, you'll 154 // start getting ids longer than 8 characters. 155 if(current < 100000000) 156 { 157 id = ("00000000"+id).substring(id.length()); 158 } 159 return id; 160 } 161 162 /*** 163 * <p> Returns a unique identifier that looks like random data. 164 * 165 * @return A String with the random looking instance identifier. 166 */ 167 public String getPseudorandomId() 168 { 169 return GenerateUniqueId.getIdentifier(); 170 } 171 }

This page was automatically generated by Maven