KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > protocol > HandleEncoder


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.protocol;
30
31 import com.caucho.ejb.AbstractServer;
32 import com.caucho.log.Log;
33 import com.caucho.util.Base64;
34 import com.caucho.util.CharBuffer;
35 import com.caucho.util.L10N;
36 import com.caucho.util.RandomUtil;
37
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * Encodes and decodes handles.
42  */

43 public class HandleEncoder {
44   private static final L10N L = new L10N(HandleEncoder.class);
45   private static final Logger JavaDoc log = Log.open(HandleEncoder.class);
46
47   private final String JavaDoc _serverId;
48   private AbstractServer _server;
49
50   public HandleEncoder(String JavaDoc serverId)
51   {
52     _serverId = serverId;
53   }
54
55   public HandleEncoder(AbstractServer server, String JavaDoc serverId)
56   {
57     this(serverId);
58
59     setServer(server);
60   }
61
62   public String JavaDoc getServerId()
63   {
64     return _serverId;
65   }
66
67   protected void setServer(AbstractServer server)
68   {
69     _server = server;
70   }
71
72   protected AbstractServer getServer()
73   {
74     return _server;
75   }
76
77   /**
78    * Creates a home handle given the server id.
79    */

80   public AbstractHomeHandle createHomeHandle()
81   {
82     if (_server != null) {
83       try {
84         return new HomeHandleImpl(_server.getEJBHome(), _serverId);
85       } catch (Throwable JavaDoc e) {
86       }
87     }
88
89     return new HomeHandleImpl(_serverId);
90   }
91
92   /**
93    * Converts the primary key to a URL.
94    */

95   public String JavaDoc getURL()
96   {
97     return _serverId;
98   }
99
100   /**
101    * Converts the primary key to a URL.
102    */

103   public String JavaDoc getURL(String JavaDoc primaryKey)
104   {
105     return _serverId + "?id=" + primaryKey;
106   }
107
108   /**
109    * Creates a handle given the server id and the object id.
110    */

111   public AbstractHandle createHandle(String JavaDoc objectId)
112   {
113     return new HandleImpl(_serverId, objectId);
114   }
115
116   /**
117    * Creates a random string key.
118    */

119   public String JavaDoc createRandomStringKey()
120   {
121     long id = RandomUtil.getRandomLong();
122     
123     CharBuffer cb = new CharBuffer();
124     Base64.encode(cb, id);
125     for (int i = 1; i < cb.length(); i++) {
126       if (cb.charAt(i) == '/')
127         cb.setCharAt(i, '-');
128     }
129     
130     return cb.toString();
131   }
132
133   /**
134    * Encodes the primary key as a string.
135    */

136   protected String JavaDoc encodePrimaryKey(Object JavaDoc primaryKey)
137   {
138     if (_server != null)
139       return _server.encodeId(primaryKey);
140     else
141       return String.valueOf(primaryKey);
142   }
143
144   public Object JavaDoc objectIdToKey(Object JavaDoc id)
145   {
146     return id;
147   }
148 }
149
Popular Tags