/************************************************************************** * 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 newPath = new ArrayList(); 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; } }