summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/lookup/Path.java
blob: 3390007bb77747512f6d8a006fc8073be9873530 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**************************************************************************
 * 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.Enumeration;
import java.util.StringTokenizer;

import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.process.Gateway;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPSearchConstraints;


/**
* @version $Revision: 1.27 $ $Date: 2006/01/17 07:49:58 $
* @author  $Author: abranson $
**/
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;

    // invalid int key
    public static final int   INVALID = -1;

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

    // slash delimited path
    protected String   mStringPath = null;
    // entity or context
    protected short    mType      = CONTEXT;
    // LDAP dn
    protected String   mDN        = null;
    // int syskey (only valid for entity SystemPaths)
    protected int      mSysKey    = INVALID;

    //
    // needed for unusual subclass constructors

    protected static String mGlobalPath; //cern.ch
    protected static String mRootPath;   //cristal2
    protected static String mLocalPath;  //lab27

    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;
        mDN = null;
        mPath = path.clone();
        mSysKey = INVALID;
    }

    /* string path e.g. /system/d000/d000/d001
     * system/domain node PRESENT
    */
    public void setPath(String path)
    {
        ArrayList<String> newPath = new ArrayList<String>();
        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;
        mDN = null;
        mSysKey = INVALID;
    }

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

    /* LDAP dn e.g. cn=6L,cn=Barrel,cn=Crystal,cn=Product,cn=domain,
     * system/domain node PRESENT
     * trailing comma
    */
    public void setDN(String dn)
    {
        // strip off root path components
        String root = "cn="+getRoot()+",";
        if (dn.endsWith(mLocalPath))
        	dn = dn.substring(0, dn.lastIndexOf(mLocalPath));

        if (dn.endsWith(root))
        	dn = dn.substring(0, dn.lastIndexOf(root));

        ArrayList<String> newPath = new ArrayList<String>();
        StringTokenizer tok = new StringTokenizer(dn, ",");
        while (tok.hasMoreTokens()) {
            String nextPath = tok.nextToken();
            if (nextPath.indexOf("cn=") == 0)
                newPath.add(0, nextPath.substring(3));
            else
                break;
        }
        mPath = (newPath.toArray(mPath));
        mSysKey = INVALID;
        mStringPath = null;
        mDN = dn+root;
    }

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

    /*
     * 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 String getDN() {
        if (mDN == null) {
            StringBuffer dnBuffer = new StringBuffer();
            for (int i=mPath.length-1; i>=0; i--)
                dnBuffer.append("cn=").append(mPath[i]).append(",");
            dnBuffer.append("cn="+getRoot()+",");
            mDN = dnBuffer.toString();
        }
        return mDN;
    }

    public String getFullDN() {
        return getDN()+mLocalPath;
    }

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

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

    public short getType() {
        return mType;
    }

    public int getSysKey() {
        return mSysKey;
    }

    public Enumeration<? extends Path> getChildren() {
        String filter = "objectclass=*";
        LDAPSearchConstraints searchCons = new LDAPSearchConstraints();
        searchCons.setBatchSize(10);
        searchCons.setDereference(LDAPSearchConstraints.DEREF_FINDING );
        return Gateway.getLookup().search(getFullDN(), LDAPConnection.SCOPE_ONE,filter,searchCons);
    }

    public Path find(String name) throws ObjectNotFoundException {
    	Enumeration<Path> e = Gateway.getLookup().search(this, name);
    	if (e.hasMoreElements()) {
    		Path thisPath = e.nextElement();
    		if (e.hasMoreElements())
    			throw new ObjectNotFoundException("More than one match for "+name, "");
    		return thisPath;
    	}
    	throw new ObjectNotFoundException("No match for "+name, "");
    }

    public abstract EntityPath getEntity() throws ObjectNotFoundException;

    //public abstract LDAPAttributeSet createAttributeSet() throws ObjectCannotBeUpdated;

    @Override
	public boolean equals( Object path )
    {
        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        dn="+getDN()+"\n        string="+toString()+"\n        int="+getSysKey()+"\n        type="+mType;
    }
}