001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.ejb.protocol;
030:
031: import com.caucho.ejb.AbstractServer;
032: import com.caucho.log.Log;
033: import com.caucho.util.Base64;
034: import com.caucho.util.CharBuffer;
035: import com.caucho.util.L10N;
036: import com.caucho.util.RandomUtil;
037:
038: import java.util.logging.Logger;
039:
040: /**
041: * Encodes and decodes handles.
042: */
043: public class HandleEncoder {
044: private static final L10N L = new L10N(HandleEncoder.class);
045: private static final Logger log = Log.open(HandleEncoder.class);
046:
047: private final String _serverId;
048: private AbstractServer _server;
049:
050: public HandleEncoder(String serverId) {
051: _serverId = serverId;
052: }
053:
054: public HandleEncoder(AbstractServer server, String serverId) {
055: this (serverId);
056:
057: setServer(server);
058: }
059:
060: public String getServerId() {
061: return _serverId;
062: }
063:
064: protected void setServer(AbstractServer server) {
065: _server = server;
066: }
067:
068: protected AbstractServer getServer() {
069: return _server;
070: }
071:
072: /**
073: * Creates a home handle given the server id.
074: */
075: public AbstractHomeHandle createHomeHandle() {
076: if (_server != null) {
077: try {
078: return new HomeHandleImpl(_server.getEJBHome(),
079: _serverId);
080: } catch (Throwable e) {
081: }
082: }
083:
084: return new HomeHandleImpl(_serverId);
085: }
086:
087: /**
088: * Converts the primary key to a URL.
089: */
090: public String getURL() {
091: return _serverId;
092: }
093:
094: /**
095: * Converts the primary key to a URL.
096: */
097: public String getURL(String primaryKey) {
098: return _serverId + "?id=" + primaryKey;
099: }
100:
101: /**
102: * Creates a handle given the server id and the object id.
103: */
104: public AbstractHandle createHandle(String objectId) {
105: return new HandleImpl(_serverId, objectId);
106: }
107:
108: /**
109: * Creates a random string key.
110: */
111: public String createRandomStringKey() {
112: long id = RandomUtil.getRandomLong();
113:
114: CharBuffer cb = new CharBuffer();
115: Base64.encode(cb, id);
116: for (int i = 1; i < cb.length(); i++) {
117: if (cb.charAt(i) == '/')
118: cb.setCharAt(i, '-');
119: }
120:
121: return cb.toString();
122: }
123:
124: /**
125: * Encodes the primary key as a string.
126: */
127: protected String encodePrimaryKey(Object primaryKey) {
128: if (_server != null)
129: return _server.encodeId(primaryKey);
130: else
131: return String.valueOf(primaryKey);
132: }
133:
134: public Object objectIdToKey(Object id) {
135: return id;
136: }
137: }
|