From: Stefan T. <th...@us...> - 2002-04-08 17:06:27
|
Update of /cvsroot/xpg-xml/edu/iicm/xpg/util In directory usw-pr-cvs1:/tmp/cvs-serv25379 Added Files: Debug.java Factory.java Log Message: new util classes --- NEW FILE: Debug.java --- /*********************************************************************** * @(#)$RCSfile: Debug.java,v $ $Revision: 1.1 $ $Date: 2002/04/08 13:08:49 $ * * Copyright (c) 2002 stefan thalauer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License (LGPL) * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ***********************************************************************/ package edu.iicm.xpg.util; // import org.dinopolis.util.Debug; public class Debug extends org.dinopolis.util.Debug { static final public String WARNING_LEVEL="Warning"; static final public String VERBOSE_LEVEL="Verbose"; static final public String PARSER_LEVEL="Parser"; static final public String TRANSITION_LEVEL="Transition"; static final public String TEST_LEVEL="Test"; } --- NEW FILE: Factory.java --- /*********************************************************************** * @(#)$RCSfile: Factory.java,v $ $Revision: 1.1 $ $Date: 2002/04/08 13:08:49 $ * * Copyright (c) 2002 stefan thalauer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License (LGPL) * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ***********************************************************************/ package edu.iicm.xpg.util; import java.io.IOException; import java.io.IOException; import java.lang.reflect.Array; import java.util.Hashtable; import java.util.HashSet; import java.util.Vector; //---------------------------------------------------------------------- /** * This is a simple factory implementation. * DFactory consists of a search method for looking up a Class Object * within a given SearchPath. The search method returns a new Instance of * the requested class. * Furthermore there is a method getSearchPath and setSearchPath to get * and set the class search path, wich consists of a stringarray of * packagenames. * * @author Peter Grundner * @author Stefan Thalauer * @version $Revision: 1.1 $ */ public class Factory { /** String Array for the class search path */ private String[] path_; /** class cache Hashtable */ private Hashtable cache_; //---------------------------------------------------------------------- /** * The default constructor */ public Factory() { cache_ = new Hashtable(); } //---------------------------------------------------------------------- /** * Another constructor with the search path as argument. * * @param path the search path */ public Factory(String[] path) { this(); setSearchPath(path); } //---------------------------------------------------------------------- /** * This method sets the seach Path and clears the class cache * to prevent the returnation of Objects from the wrong packages * * @param path represents the SearchPath * @return void * @exception IllegalArgumentException thrown if path is null * or has length 0 */ public void setSearchPath(String[] path) throws IllegalArgumentException { if (path == null) throw new IllegalArgumentException("SearchPath must not be <null>"); if (path.length == 0) throw new IllegalArgumentException("the length of SearchPath must not be 0"); Vector vector = new Vector(); HashSet set= new HashSet(); for(int count=0;count<path.length;count++) { if (set.add(path[count])) { vector.add(path[count]); } } path_ = new String[vector.size()]; vector.toArray(path_); clearClassCache(); } //---------------------------------------------------------------------- /** * This method returns the current search path and throws an * IllegalStateException if the search path is null * * @return returns the current search path as an array of strings. Each * string represents on a single package, and packages do never hav a * trailing '.' * @exception IllegalStateException thrown if no path has been * set before */ public String[] getSearchPath() throws IllegalStateException { /** check if the class search path has been set before or throw an exception */ if (path_==null) { throw new IllegalStateException(); } return path_; } //---------------------------------------------------------------------- /** * getInstance(String partly_qualified_classname) tries to find an object a * in one of the packagenames within the search path and returns * a new Instance of the found object or throws a ClassNotFoundException. * * @param partly_qualified_classname here can be a classname with or * without any sub-package information * @return an instance of the class defined by the parameter * @exception ClassNotFoundException requested class is not * found in the path * @exception LinkageError thrown if class dependencies could * not be resolved * @exception ExceptionInInitializerError thrown if the default * constructor could not be called, eiher because it does not exist or * because it is not accessible (e.g. private) * @exception IllegalArgumentException thrown if * partly_qualified_classname was null * @exception IllegalStateException thrown if no path has been * set before * @exception IOException something went wrong during reading * the class code */ public Object getInstance(String partly_qualified_classname) throws ClassNotFoundException, LinkageError, ExceptionInInitializerError, IllegalArgumentException, IllegalStateException, IOException { // search within cache Class ret_class = (Class)(cache_.get(partly_qualified_classname)); if (Debug.DEBUG) Debug.println(Debug.VERBOSE_LEVEL," return class = "+ret_class); try { if (ret_class != null) return(ret_class.newInstance()); // walk through the whole class search path for (int count = 0; count < path_.length; count++) { StringBuffer buf = new StringBuffer(); if (Debug.DEBUG) Debug.println(Debug.VERBOSE_LEVEL," path__ = "+path_[count]); buf.append(path_[count]); buf.append('.'); buf.append(partly_qualified_classname); String full_class_name = buf.toString(); if (Debug.DEBUG) Debug.println(Debug.VERBOSE_LEVEL," full_class_name = "+full_class_name); try { ret_class = Class.forName(full_class_name); if (Debug.DEBUG) Debug.println(Debug.VERBOSE_LEVEL," ret_class = "+ret_class); cache_.put(partly_qualified_classname, ret_class); Object obj = ret_class.newInstance(); if (obj == null) { if (Debug.DEBUG) Debug.println(Debug.WARNING_LEVEL," Object is null!"); } else if (Debug.DEBUG) Debug.println(Debug.VERBOSE_LEVEL," object = "+obj); return(ret_class.newInstance()); } catch (ClassNotFoundException e) { } } throw(new ClassNotFoundException(partly_qualified_classname)); } catch (InstantiationException e) { // FIXXME // an InstantiationException should be declared in the // signature of this method throw new ExceptionInInitializerError(); } catch (IllegalAccessException e) { // FIXXME // an IllegalAccessException should be declared in the // signature of this method throw new ExceptionInInitializerError(); } } //---------------------------------------------------------------------- /** * Just for clearing the cache... * * @return */ public void clearClassCache() { cache_.clear(); } // FIXXME (Stefan Thalauer, Apr 4 2002 15:12) -> searchpath methodes //---------------------------------------------------------------------- /** * This method returns the position the path * * @param path * @return returns the index if the new path allready exists in the search * path, -1 if not * @exception IllegalStateException thrown if no path has been * set before */ public int getSearchPathIndex(String path) throws IllegalStateException { if (path_==null) { throw new IllegalStateException(); } for(int count=0; count < path_.length ; count++) { if ( path_[count].equals(path) ) return count; } return -1; } //---------------------------------------------------------------------- /** * This method checks if a path is in the current SearchPath * * @param path * @return returns true if the new path allready exists in the search * path * @exception IllegalStateException thrown if no path has been * set before */ public boolean isSearchPathRegistered(String path) throws IllegalStateException { if (getSearchPathIndex(path) == -1) return false; else return true; } //---------------------------------------------------------------------- /** * This method appends a path to the current SearchPath. * * @param path * @exception IllegalArgumentException */ public void appendSearchPath(String path) throws IllegalStateException,IllegalArgumentException { if (path_==null) throw new IllegalStateException(); if (path == null) throw new IllegalArgumentException("The Path must not be <null>"); insertPath(path,path_.length); } //---------------------------------------------------------------------- /** * This method inserts the path at the position in the current SearchPath. * * @param path * @param position * @exception IllegalStateException thrown if no path has been * set before * @exception IllegalArgumentException */ public void insertSearchPath(String path,int position) throws IllegalStateException,IllegalArgumentException { if (path_==null) throw new IllegalStateException(); if (path == null) throw new IllegalArgumentException("The Path must not be <null>"); if ( position < 0 | position > path_.length) throw new IllegalArgumentException("The Position is out of the Path boundary"); insertPath(path,position); clearClassCache(); } //---------------------------------------------------------------------- /** * This method inserts the path at the position in the current SearchPath. * * @param path * @param position * @exception IllegalStateException thrown if no path has been * set before * @exception IllegalArgumentException */ protected void insertPath(String path,int position) throws IllegalStateException,IllegalArgumentException { if (isSearchPathRegistered(path)) { if (Debug.DEBUG) Debug.println(Debug.WARNING_LEVEL,"Path: \"" + path +"\" allready registered in the Search Path"); } else { String[] new_path = new String [path_.length + 1]; for (int count = 0; count< position;count++) { new_path[count]=path_[count]; } new_path[position]=path; for (int count = position; count< path_.length;count++) { new_path[count+1]=path_[count]; } path_ = new_path; } } //---------------------------------------------------------------------- /** * This method removes the path to the SearchPath. * * @param path * @exception IllegalStateException thrown if no path has been * set before * @exception IllegalArgumentException */ public void removeSearchPath(String path) throws IllegalStateException,IllegalStateException { if (path == null) throw new IllegalArgumentException("The Path must not be <null>"); int position = getSearchPathIndex(path); if (position < 0) { if (Debug.DEBUG) Debug.println(Debug.WARNING_LEVEL,"Path: \"" + path +"\" is not registered in the Search Path "); } else { removeSearchPath(position); } } //---------------------------------------------------------------------- /** * This method removes the last path of the SearchPath. * * @exception IllegalStateException thrown if no path has been * set before * @exception IllegalArgumentException */ public void removeLastSearchPath() throws IllegalStateException { if (path_==null) throw new IllegalStateException(); removeSearchPath(path_.length-1); } //---------------------------------------------------------------------- /** * This method removes the path on a specified position of the SearchPath. * * @param position * @exception IllegalStateException thrown if no path has been * set before * @exception IllegalArgumentException */ public void removeSearchPath(int position) throws IllegalStateException,IllegalArgumentException { if (path_==null) throw new IllegalStateException(); if ( path_.length == 0) { if (Debug.DEBUG) Debug.println(Debug.WARNING_LEVEL,"There are no Path in the Search Path"); } else { if ( position < 0 | position > path_.length) throw new IllegalArgumentException("The Position is out of the Path boundary"); String[] new_path = new String [path_.length -1]; for (int count = 0; count< position;count++) { new_path[count]=path_[count]; } for (int count = position; count< new_path.length;count++) { new_path[count]=path_[count+1]; } path_ = new_path; clearClassCache(); } } //---------------------------------------------------------------------- /** * This method reverse the strings in the string array. * * @exception IllegalStateException thrown if no path has been * set before */ public void reverseSearchPath() throws IllegalStateException { if (path_==null) throw new IllegalStateException(); String[] new_path = new String [path_.length]; for(int count = 0;count < path_.length;count++) { new_path[count]=path_[path_.length - 1 - count]; } path_=new_path; clearClassCache(); } // END FIXXME (Stefan Thalauer, Feb14 2002 22:32) } |