KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > mobilitools > smi > api > Location


1 /*
2 * MobiliTools: an implementation of the Object Management Group's
3 * Mobile Agent Facility specification.
4 * Copyright (C) 2003 France Telecom R&D
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * MobiliTools $Name: $
21 *
22 * Contact: mobilitools-smi@lists.debian-sf.objectweb.org
23 *
24 * Authors: Bruno Dillenseger
25 */

26
27
28 package org.objectweb.mobilitools.smi.api;
29
30
31 import java.io.Serializable JavaDoc;
32
33 /**
34  * MobiliTools $Name: $, $Id: Location.java,v 1.1.1.1 2003/03/28 14:48:06 dillense Exp $
35  * <P>
36  * SMI locations are based on a special naming scheme, based on CORBA naming service
37  * (see COS naming URI in MAF specification):
38  * CosNaming:region_prefix/region_name/agency_prefix/agency_name
39  * e.g. CosNaming:MAF/my_region/agency/my_agency
40  * Default prefixes are defined in Constants class, and can be overridden by
41  * setting appropriate properties.
42 */

43 public class Location implements Serializable JavaDoc
44 {
45     static String JavaDoc regionPrefix = System.getProperty(Constants.regionPrefixProp, Constants.regionPrefixDefault);
46     static String JavaDoc agencyPrefix = System.getProperty(Constants.agencyPrefixProp, Constants.agencyPrefixDefault);
47     String JavaDoc my_region;
48     String JavaDoc my_agency;
49     String JavaDoc my_location;
50
51
52     /**
53         Creates a new Location object from the provided region name and agency name.
54     */

55     public Location(String JavaDoc region, String JavaDoc agency)
56     {
57         my_region = region;
58         my_agency = agency;
59         my_location =
60             Constants.COSNAMINGURI +
61             regionPrefix +
62             "/" +
63             region +
64             "/" +
65             agencyPrefix +
66             "/" +
67             agency;
68     }
69
70
71     /**
72         Creates a new Location object from the provided location string.
73         String format should be: CosNaming:&lt;region_prefix&gt;/&lt;region_name&gt;/&lt;agency_prefix&gt;/&lt;agency_name&gt;,
74         according to default prefixes defined in Constants class, or according to properties overriding
75         these default prefixes.
76         @see Constants
77         @throws BadOperation the provided string format is not recognized as a location.
78     */

79     public Location(String JavaDoc location)
80         throws BadOperation
81     {
82         if (location == null)
83         {
84             throw new BadOperation(BadOperation.REJECTED, location + " is not a location string.");
85         }
86         my_location = location;
87         if (! location.startsWith(Constants.COSNAMINGURI))
88         {
89             throw new BadOperation(BadOperation.REJECTED, location + " is not a location string.");
90         }
91         location = location.substring(Constants.COSNAMINGURI.length());
92         if (! location.startsWith(regionPrefix + "/"))
93         {
94             throw new BadOperation(BadOperation.REJECTED, location + " is not a location string.");
95         }
96         if (regionPrefix.length() > 0)
97         {
98             location = location.substring(regionPrefix.length()+1);
99         }
100         int beginAgency = location.lastIndexOf("/");
101         if (beginAgency == -1 || beginAgency == location.length()-1)
102         {
103             throw new BadOperation(BadOperation.REJECTED, location + " is not a location string.");
104         }
105         my_agency = location.substring(beginAgency + 1);
106         try
107         {
108             if (agencyPrefix.length() > 0)
109             {
110                 my_region = location.substring(0, beginAgency - agencyPrefix.length() - 1);
111             }
112             else
113             {
114                 my_region = location.substring(0, beginAgency);
115             }
116         }
117         catch (IndexOutOfBoundsException JavaDoc e)
118         {
119             throw new BadOperation(BadOperation.REJECTED, location + " is not a location string.");
120         }
121     }
122
123
124     /**
125         @return region name for this location.
126     */

127     public String JavaDoc getRegion()
128     {
129         return my_region;
130     }
131
132
133     /**
134        @return agency name for this location.
135     */

136     public String JavaDoc getAgency()
137     {
138         return my_agency;
139     }
140
141
142     public String JavaDoc toString()
143     {
144         return my_location;
145     }
146
147
148     public boolean equals(Object JavaDoc other)
149     {
150         if (other instanceof Location)
151         {
152             return
153                 my_region.equals(((Location)other).getRegion()) &&
154                 my_agency.equals(((Location)other).getAgency());
155         }
156         else
157         {
158             return false;
159         }
160     }
161
162
163     /**
164         @return location's mapping to a string'fied name in CORBA naming service.
165     */

166     public String JavaDoc getCosNamingName()
167     {
168         return my_location.substring(Constants.COSNAMINGURI.length());
169     }
170 }
171
Popular Tags