/************************************************************************** * 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.Iterator; import java.util.StringTokenizer; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.process.Gateway; /** * @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 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 newPath = new ArrayList(); 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 newPath = new ArrayList(); 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 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 { Iterator e = Gateway.getLookup().search(this, name); if (e.hasNext()) { Path thisPath = e.next(); if (e.hasNext()) 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; } }