View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape 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. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.web.jcr.rest.client.domain;
25  
26  import net.jcip.annotations.Immutable;
27  
28  import org.modeshape.common.util.CheckArg;
29  import org.modeshape.common.util.HashCode;
30  import org.modeshape.web.jcr.rest.client.RestClientI18n;
31  import org.modeshape.web.jcr.rest.client.Utils;
32  
33  /**
34   * The <code>Server</code> class is the business object for a server that is hosting one or more ModeShape repositories.
35   * 
36   * <p>
37   * The server requires a <i>url</i>, <i>user</i> name, and a <i>password</i> in order to connect.
38   * The {@link #url} that is used has a format of <b>http://hostname:port/context root</b>.  
39   * Where 
40   * <li>hostname is the name of the server</li>
41   * <li>port is the port to connect to, generally its 8080</li>
42   * <li>context root is the deployed war context</li>
43   * <p>
44   * The deployed war context root is based on what the deployed war file is called.
45   * If the ModeShape deployed war is called resources.war (which is the default build name), then the context root 
46   * would be <i>resources</i>.  
47   */
48  @Immutable
49  public class Server implements IModeShapeObject {
50  
51      // ===========================================================================================================================
52      // Fields
53      // ===========================================================================================================================
54  
55      /**
56       * The password to use when logging on to the server.
57       */
58      private final String password;
59  
60      /**
61       * The server URL (never <code>null</code>).
62       */
63      private final String url;
64  
65      /**
66       * The user name to use when logging on to the server (never <code>null</code>).
67       */
68      private final String user;
69  
70      // ===========================================================================================================================
71      // Constructors
72      // ===========================================================================================================================
73  
74      /**
75       * Constructs on new <code>Server</code>.
76       * 
77       * @param url the server URL, which must contain the deployed war file context (never <code>null</code>)
78       * @param user the server user (may be <code>null</code>)
79       * @param password the server password (may be <code>null</code>)
80       * @throws IllegalArgumentException if the URL or user arguments are <code>null</code>
81       */
82      public Server( String url,
83                     String user,
84                     String password ) {
85      	assert url != null;
86      	assert user != null;
87  
88          this.url = url;
89          this.user = user;
90          this.password = password;
91      }
92  
93      // ===========================================================================================================================
94      // Methods
95      // ===========================================================================================================================
96  
97      /**
98       * {@inheritDoc}
99       * 
100      * @see java.lang.Object#equals(java.lang.Object)
101      */
102     @Override
103     public boolean equals( Object obj ) {
104         if (this == obj) return true;
105         if ((obj == null) || (getClass() != obj.getClass())) return false;
106 
107         Server otherServer = (Server)obj;
108         return Utils.equivalent(this.url, otherServer.url) && Utils.equivalent(this.user, otherServer.user)
109                && Utils.equivalent(this.password, otherServer.password);
110     }
111 
112     /**
113      * {@inheritDoc}
114      * 
115      * @see org.modeshape.web.jcr.rest.client.domain.IModeShapeObject#getName()
116      */
117     public String getName() {
118         return getUrl();
119     }
120 
121     /**
122      * @return the server authentication password
123      */
124     public String getPassword() {
125         return this.password;
126     }
127 
128     /**
129      * {@inheritDoc}
130      * 
131      * @see org.modeshape.web.jcr.rest.client.domain.IModeShapeObject#getShortDescription()
132      */
133     public String getShortDescription() {
134         return RestClientI18n.serverShortDescription.text(this.url, this.user);
135     }
136 
137     /**
138      * @return the server URL (never <code>null</code>)
139      */
140     public String getUrl() {
141         return this.url;
142     }
143 
144     /**
145      * @return the server authentication user (never <code>null</code>)
146      */
147     public String getUser() {
148         return this.user;
149     }
150 
151     /**
152      * {@inheritDoc}
153      * 
154      * @see java.lang.Object#hashCode()
155      */
156     @Override
157     public int hashCode() {
158         return HashCode.compute(this.url, this.user, this.password);
159     }
160 
161     /**
162      * A server has the same identifying properties if their URL and user matches.
163      * 
164      * @param otherServer the server whose key is being compared (never <code>null</code>)
165      * @return <code>true</code> if the servers have the same key
166      * @throws IllegalArgumentException if the argument is <code>null</code>
167      */
168     public boolean hasSameKey( Server otherServer ) {
169         CheckArg.isNotNull(otherServer, "otherServer");
170         return (Utils.equivalent(this.url, otherServer.url) && Utils.equivalent(this.user, otherServer.user));
171     }
172 
173     /**
174      * {@inheritDoc}
175      * 
176      * @see java.lang.Object#toString()
177      */
178     @Override
179     public String toString() {
180         return getShortDescription();
181     }
182 
183 }