summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/lookup/Path.java
blob: 1a563b5f0ca1c02eca2d9e64e0cc30fe01bb9153 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**************************************************************************
 * Path.java
 *
 * $Revision: 1.27 $
 * $Date: 2006/01/17 07:49:58 $
 *
 * Copyright (C) 2001 CERN - European Organization for Nuclear Research
 * All rights reserved.
 **************************************************************************/

package com.c2kernel.lookup;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.UUID;

import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.common.SystemKey;
import com.c2kernel.process.Gateway;


/**
* 
**/
public abstract class Path implements Serializable
{
    public static final String delim = "/";

    // types
    public static final short UNKNOWN = 0;
    public static final short CONTEXT = 1;
    public static final short ENTITY  = 2;

    protected String[] mPath = new String[0];

    // slash delimited path
    protected String   mStringPath = null;
    // entity or context
    protected short    mType      = CONTEXT;
    
    // item UUID (only valid for ItemPaths and DomainPaths that are aliases for Items)
    protected UUID			 mUUID;
    protected SystemKey		 mSysKey;

    // ior is stored in here when it is resolved
    protected org.omg.CORBA.Object   mIOR         = null;

    public Path() {
    }

    /*
    * Creates an empty path
    */
    public Path(short type)
    {
        mType = type;
    }

    /*
    * Creates a path with an arraylist of the path (big endian)
    */
    public Path(String[] path, short type) {
        setPath(path);
        mType = type;
    }

    /*
     * Creates a path from a slash separated string (big endian)
    */
    public Path(String path, short type) {
        setPath(path);
        mType = type;
    }

    /*
     *  Create a path by appending a child string to an existing path
     */
    public Path(Path parent, String child, short type) {
        String[] oldPath = parent.getPath();
        mPath = new String[oldPath.length+1];
        for (int i=0; i<oldPath.length; i++)
            mPath[i] = oldPath[i];
        mPath[oldPath.length] = child;
        mType = type;
    }

     /*
      * Create a path by appending a child and inheriting the type
      */
    public Path(Path parent, String child) {
        this(parent, child, UNKNOWN);
    }
    /*************************************************************************/

    // Setters

    /* string array path e.g. { "Product", "Crystal", "Barrel", "2L", "331013013348" }
     * system/domain node ABSENT
    */
    public void setPath(String[] path)
    {
        mStringPath = null;
        mPath = path.clone();
        mUUID = null;
        mSysKey = null;
    }

    /* string path e.g. /system/d000/d000/d001
     * system/domain node PRESENT
    */
    public void setPath(String path)
    {
        ArrayList<String> newPath = new ArrayList<String>();
        if (path != null) {
        	StringTokenizer tok = new StringTokenizer(path, delim);
	        if (tok.hasMoreTokens()) {
		        String first = tok.nextToken();
		        if (!first.equals(getRoot()))
		        	newPath.add(first);
		        while (tok.hasMoreTokens())
		            newPath.add(tok.nextToken());
	        }
        }

        mPath = (newPath.toArray(mPath));
        mStringPath = null;
        mUUID = null;
        mSysKey = null;
    }

    // lookup sets the IOR
    public void setIOR(org.omg.CORBA.Object IOR) {
        mIOR = IOR;
        if (IOR == null) mType = Path.CONTEXT;
        else mType = Path.ENTITY;
    }

    /* clone another path object
    */
    public void setPath(Path path)
    {
        mStringPath = null;
        mPath = (path.getPath().clone());
        mUUID = null;
        mSysKey = null;
    }

    /*************************************************************************/


    /*
     * Getter Methods
     */

     // root is defined as 'domain', 'entity' or 'system' in subclasses
    public abstract String getRoot();

    public String[] getPath()
    {
        return mPath;
    }

    public String getString()
    {
        if (mStringPath == null) {
                StringBuffer stringPathBuffer = new StringBuffer("/").append(getRoot());
                for (String element : mPath)
					stringPathBuffer.append(delim).append(element);
                mStringPath = stringPathBuffer.toString();
        }
        return mStringPath;
    }

    public boolean exists() {
        return Gateway.getLookup().exists(this);
    }

    /** Queries the lookup for the IOR
     */

    public org.omg.CORBA.Object getIOR() {
        org.omg.CORBA.Object newIOR = null;
        if (mIOR==null) { // if not cached try to resolve
            Lookup myLookup = Gateway.getLookup();
            try {
                newIOR = myLookup.resolve(this);
            } catch (ObjectNotFoundException ex) {
            }
            setIOR(newIOR);
        }
        return mIOR;
    }

    @Override
	public String toString() {
        return getString();
    }

    public short getType() {
        return mType;
    }

    public SystemKey getSystemKey() {
        return mSysKey;
    }
    
    public UUID getUUID() {
    	return mUUID;
    }

    public abstract ItemPath getItemPath() throws ObjectNotFoundException;

    @Override
	public boolean equals( Object path )
    {
    	if (path == null) return false;
        return toString().equals(path.toString());
    }

    @Override
	public int hashCode() {
        return toString().hashCode();
    }

    public String dump() {
        StringBuffer comp = new StringBuffer("Components: { ");
        for (String element : mPath)
			comp.append("'").append(element).append("' ");
        return "Path - dump(): "+comp.toString()+"}\n        string="+toString()+"\n        uuid="+getUUID()+"\n        type="+mType;
    }
}