1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* 
18  
19   */
20  
21  package org.apache.wsrp4j.util;
22  
23  import java.net.InetAddress;
24  import java.util.Random;
25  
26  /**
27   * This class implements the HandleGenerator interface.
28   * 
29   * @author Stephan Laertz
30   *
31   * @version 1.1
32   * 
33   * @see     HandleGenerator 
34   *
35   */
36  public class HandleGeneratorImpl implements HandleGenerator {
37  
38      private static int counter = 0;
39  
40      /**
41       * Private constructor of HandleGeneratorImpl.
42       */
43      private HandleGeneratorImpl() {
44      }
45  
46      /**
47       * Returns an instance of HandleGeneratorImpl.
48       *
49       * @return String representing the new handle.
50       */
51      public static HandleGeneratorImpl create() {
52          return new HandleGeneratorImpl();
53      }
54  
55      /**
56       * Generates a new handle and returns it as a String.
57       *
58       * @return String representing the new handle.
59       */
60      synchronized public String generateHandle() {
61  
62          String host = null;
63          try {
64  
65              host = InetAddress.getLocalHost().getHostAddress().toString();
66  
67          }
68          catch (Exception e) {
69  
70              host = Double.toString(new Random(new Random().nextLong())
71                      .nextDouble());
72  
73          }
74  
75          StringBuffer handle = new StringBuffer(host);
76          handle.append("_");
77          handle.append(String.valueOf(System.currentTimeMillis()));
78          handle.append("_");
79          handle.append(Integer.toString(counter++));
80  
81          return handle.toString();
82      }
83  
84  }