001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.objectserver.managedobject;
005:
006: import com.tc.object.ObjectID;
007: import com.tc.object.SerializationUtil;
008: import com.tc.object.dna.api.DNA;
009: import com.tc.object.dna.api.DNACursor;
010: import com.tc.object.dna.api.DNAWriter;
011: import com.tc.object.dna.api.LogicalAction;
012: import com.tc.objectserver.mgmt.ManagedObjectFacade;
013: import com.tc.objectserver.mgmt.PhysicalManagedObjectFacade;
014: import com.tc.util.Assert;
015:
016: import java.io.IOException;
017: import java.io.ObjectInput;
018: import java.io.ObjectOutput;
019: import java.net.MalformedURLException;
020: import java.net.URL;
021: import java.util.HashMap;
022: import java.util.Map;
023: import java.util.Set;
024:
025: /**
026: * ManagedObjectState for URLs.
027: */
028: public class URLManagedObjectState extends LogicalManagedObjectState {
029:
030: private String protocol = null;
031: private String host = null;
032: private int port = -1;
033: private String authority = null;
034: private String userInfo = null;
035: private String path = null;
036: private String query = null;
037: private String ref = null;
038:
039: public URLManagedObjectState(long classID) {
040: super (classID);
041: }
042:
043: protected URLManagedObjectState(ObjectInput in) throws IOException {
044: super (in);
045: }
046:
047: public void apply(ObjectID objectID, DNACursor cursor,
048: BackReferences includeIDs) throws IOException {
049: while (cursor.next()) {
050: LogicalAction action = cursor.getLogicalAction();
051: int method = action.getMethod();
052: Object[] params = action.getParameters();
053:
054: switch (method) {
055: case SerializationUtil.URL_SET:
056: Assert.assertNotNull(params[0]);
057: Assert.assertNotNull(params[1]);
058: Assert.assertNotNull(params[2]);
059: Assert.assertNotNull(params[3]);
060: Assert.assertNotNull(params[4]);
061: Assert.assertNotNull(params[5]);
062: Assert.assertNotNull(params[6]);
063: Assert.assertNotNull(params[7]);
064: if (!ObjectID.NULL_ID.equals(params[0])) {
065: protocol = params[0].toString();
066: }
067: if (!ObjectID.NULL_ID.equals(params[1])) {
068: host = params[1].toString();
069: }
070: if (!ObjectID.NULL_ID.equals(params[2])) {
071: port = ((Integer) params[2]).intValue();
072: }
073: if (!ObjectID.NULL_ID.equals(params[3])) {
074: authority = params[3].toString();
075: }
076: if (!ObjectID.NULL_ID.equals(params[4])) {
077: userInfo = params[4].toString();
078: }
079: if (!ObjectID.NULL_ID.equals(params[5])) {
080: path = params[5].toString();
081: }
082: if (!ObjectID.NULL_ID.equals(params[6])) {
083: query = params[6].toString();
084: }
085: if (!ObjectID.NULL_ID.equals(params[7])) {
086: ref = params[7].toString();
087: }
088: break;
089: default:
090: throw new AssertionError("Invalid action:" + method);
091: }
092: }
093: }
094:
095: public void dehydrate(ObjectID objectID, DNAWriter writer) {
096: writer.addLogicalAction(SerializationUtil.URL_SET,
097: new Object[] { protocol, host, new Integer(port),
098: authority, userInfo, path, query, ref });
099: }
100:
101: protected void addAllObjectReferencesTo(Set refs) {
102: return;
103: }
104:
105: private URL createURLFromState() {
106: String file = path;
107: if (query != null && query.length() > 0) {
108: file = file + "?" + query;
109: }
110: if (ref != null && ref.length() > 0) {
111: file = file + "#" + ref;
112: }
113:
114: try {
115: return new URL(protocol, host, port, file);
116: } catch (MalformedURLException e) {
117: throw new RuntimeException(e);
118: }
119: }
120:
121: public ManagedObjectFacade createFacade(ObjectID objectID,
122: String className, int limit) {
123: Map dataCopy = new HashMap();
124: dataCopy.put("url", createURLFromState());
125: return new PhysicalManagedObjectFacade(objectID,
126: ObjectID.NULL_ID, className, dataCopy, false,
127: DNA.NULL_ARRAY_SIZE, false);
128: }
129:
130: protected void basicWriteTo(ObjectOutput o) throws IOException {
131: o.writeUTF(protocol);
132: o.writeUTF(host);
133: o.writeInt(port);
134: o.writeUTF(authority);
135: o.writeUTF(userInfo);
136: o.writeUTF(path);
137: o.writeUTF(query);
138: o.writeUTF(ref);
139: }
140:
141: static URLManagedObjectState readFrom(ObjectInput in)
142: throws IOException {
143: URLManagedObjectState state = new URLManagedObjectState(in);
144: state.protocol = in.readUTF();
145: state.host = in.readUTF();
146: state.port = in.readInt();
147: state.authority = in.readUTF();
148: state.userInfo = in.readUTF();
149: state.path = in.readUTF();
150: state.query = in.readUTF();
151: state.ref = in.readUTF();
152: return state;
153: }
154:
155: protected boolean basicEquals(LogicalManagedObjectState o) {
156: URLManagedObjectState dms = (URLManagedObjectState) o;
157: return dms.protocol.equals(protocol) && dms.host.equals(host)
158: && dms.port == port && dms.authority.equals(authority)
159: && dms.userInfo.equals(userInfo)
160: && dms.path.equals(path) && dms.query.equals(query)
161: && dms.ref.equals(ref);
162: }
163:
164: public byte getType() {
165: return URL_TYPE;
166: }
167: }
|