added the ability to list all modules in the module directory

This commit is contained in:
Eric J. Bowersox 2003-06-17 08:28:13 +00:00
parent 835c9c389f
commit dfb73d88ff
2 changed files with 88 additions and 0 deletions

View File

@ -20,6 +20,7 @@ package com.silverwrist.dynamo.module;
import java.io.*;
import java.net.MalformedURLException;
import java.util.*;
import java.util.jar.*;
import org.w3c.dom.*;
import com.silverwrist.util.xml.*;
import com.silverwrist.dynamo.Namespaces;
@ -53,6 +54,61 @@ public class ModuleManager implements NamedObject, ComponentInitialize, Componen
{ // do nothing
} // end constructor
/*--------------------------------------------------------------------------------
* Internal operations
*--------------------------------------------------------------------------------
*/
private static final void pushFileList(LinkedList list, File file)
{
File[] subfiles = file.listFiles();
if (subfiles==null)
return;
Arrays.sort(subfiles);
for (int i=subfiles.length-1; i>=0; i--)
list.addFirst(subfiles[i]);
} // end pushFileList
private static final boolean isDynamoModule(File f)
{
JarFile jf = null;
try
{ // open this file as a JAR file
jf = new JarFile(f);
Manifest mft = jf.getManifest();
if (mft==null)
return false;
Attributes attrs = mft.getMainAttributes();
if (attrs==null)
return false;
if (attrs.getValue("X-Dynamo-Module-Class")!=null)
return true;
} // end try
catch (IOException e)
{ // if there's an exception, we just bail
return false;
} // end catch
finally
{ // clean up before we go
try
{ // close the file
if (jf!=null)
jf.close();
} // end try
catch (IOException e)
{ // ignore exceptions
} // end catch
} // end finally
return false;
} // end isDynamoModule
/*--------------------------------------------------------------------------------
* Implementations from interface NamedObject
*--------------------------------------------------------------------------------
@ -255,4 +311,33 @@ public class ModuleManager implements NamedObject, ComponentInitialize, Componen
} // end freeUnusedModules
public List listAllModuleNames()
{
ArrayList rc = new ArrayList();
int len = m_mod_dir.getAbsolutePath().length();
LinkedList process = new LinkedList();
pushFileList(process,m_mod_dir);
while (process.size()>0)
{ // examine all files
File f = (File)(process.removeFirst());
if (f.isDirectory())
pushFileList(process,f); // list files in the subdirectory
else if (isDynamoModule(f))
{ // strip off the base path and return the name
String name = f.getAbsolutePath().substring(len);
if (name.startsWith(File.pathSeparator))
name = name.substring(File.pathSeparator.length());
rc.add(name);
} // end else if
} // end while
if (rc.isEmpty())
return Collections.EMPTY_LIST;
rc.trimToSize();
return Collections.unmodifiableList(rc);
} // end listAllModuleNames
} // end class ModuleManager

View File

@ -17,6 +17,7 @@
*/
package com.silverwrist.dynamo.module;
import java.util.List;
import com.silverwrist.dynamo.except.ModuleException;
import com.silverwrist.dynamo.iface.Module;
@ -28,4 +29,6 @@ public interface ModuleOperations
public void freeUnusedModules();
public List listAllModuleNames();
} // end interface ModuleOperations