1 package org.xvsm.core;
2
3 import java.io.IOException;
4 import java.io.Serializable;
5 import java.net.URI;
6 import java.net.URISyntaxException;
7 import java.util.UUID;
8
9 import org.xvsm.configuration.ConfigurationManager;
10 import org.xvsm.interfaces.container.IContainer;
11 import org.xvsm.internal.exceptions.FatalException;
12 import org.xvsm.remote.TransportHandler;
13
14 /***
15 * TODO.
16 *
17 * @author Christian Schreiber, Michael Proestler
18 *
19 */
20 public class ContainerRef implements Comparable, Serializable {
21 /***
22 * Auto genereated serial version uid.
23 */
24 private static final long serialVersionUID = 5812673094573161732L;
25
26 /***
27 * The identifier.
28 */
29 private String id;
30
31 /***
32 * the uri for the MozartSpaces instance.
33 */
34 private URI site = null;
35
36 /***
37 * The container for this cref.
38 */
39 private IContainer container = null;
40
41 /***
42 * Default Constructor.
43 */
44 public ContainerRef() {
45 this(UUID.randomUUID().toString());
46 }
47
48 /***
49 * Creates a new ContainerRef.
50 *
51 * @param uri
52 * the container reference of the new container as uri (e.g.
53 * <code>tcpjava://mozartspaces.org/cref</code).
54 */
55 public ContainerRef(URI uri) {
56 super();
57 try {
58 this.site = new URI(uri.getScheme() + "://" + uri.getAuthority());
59 String path = uri.getPath();
60 if (path.equals("/lookup")) {
61 id = "lookup";
62 } else {
63 this.id = path.substring("/containers/".length());
64 }
65 } catch (URISyntaxException e) {
66 throw new FatalException(e);
67 }
68 }
69
70 /***
71 * Create a new ContainerRef.
72 *
73 * @param id
74 * the identifier of this ContainerRef.
75 */
76 public ContainerRef(String id) {
77 super();
78 this.id = id;
79 }
80
81 /***
82 *
83 * @return the id of this containerref.
84 */
85 public String getId() {
86 return id;
87 }
88
89 /***
90 * @param id
91 * the id to set
92 */
93 public void setId(String id) {
94 this.id = id;
95 }
96
97 /***
98 * {@inheritDoc}
99 */
100 @Override
101 public int hashCode() {
102 final int prime = 31;
103 int result = 1;
104 result = prime * result + ((id == null) ? 0 : id.hashCode());
105 return result;
106 }
107
108 /***
109 * {@inheritDoc}
110 */
111 @Override
112 public boolean equals(Object obj) {
113 return obj instanceof ContainerRef
114 && this.id.equals(((ContainerRef) obj).getId());
115 }
116
117 /***
118 * Returns the uri representation of the container.<br>
119 * Format: <code>
120 * <protocol>://<host>:<port>/containers/<identifier>
121 *
122 * @return
123 */
124 public URI asURI() {
125 String infix = "/";
126 if (!this.id.equals("lookup")) {
127 infix = "/containers/";
128 }
129 try {
130 if (this.site == null) {
131 URI uri = TransportHandler.getInstance().getListener(
132 ConfigurationManager.getInstance().getStringSetting(
133 "DefaultAnswerToProtocol")).getUri();
134 return new URI(uri.toASCIIString() + infix + this.id);
135 } else {
136 return new URI(this.site.toASCIIString() + infix + this.id);
137 }
138 } catch (URISyntaxException e) {
139 throw new FatalException(e);
140 }
141 }
142
143 /***
144 * @return the uri
145 */
146 public URI getSite() {
147 return site;
148 }
149
150 /***
151 * @param uri
152 * the uri to set
153 */
154 public void setSite(URI site) {
155 this.site = site;
156 }
157
158 /***
159 * {@inheritDoc}
160 */
161
162 public int compareTo(Object o) {
163 if (o instanceof ContainerRef) {
164 ContainerRef r = (ContainerRef) o;
165 return r.getId().compareTo(this.id);
166 }
167 return 0;
168 }
169
170 /***
171 * @return the container
172 */
173 public IContainer getContainer() {
174 return container;
175 }
176
177 /***
178 * @param container
179 * the container to set
180 */
181 public void setContainer(IContainer container) {
182 this.container = container;
183 }
184
185 /***
186 * {@inheritDoc}
187 */
188 @Override
189 public String toString() {
190 return this.getId();
191 }
192
193 /***
194 * Method is used for seializing the ContainerRef.<br>
195 * We have to remove the reference to the container.
196 *
197 * @param out
198 * the ObjectOutputStream.
199 * @throws IOException
200 * thrown if problems accure while writing on out.
201 * @see {@link java.io.Serializable}
202 */
203 private void writeObject(java.io.ObjectOutputStream out) throws IOException {
204 this.container = null;
205 out.defaultWriteObject();
206 }
207 }