001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * any nuclear facility.
035: */
036:
037: package com.sun.portal.search.providers.faces;
038:
039: import java.io.File;
040: import java.net.URL;
041: import java.util.ArrayList;
042:
043: public class pluginLoader {
044:
045: /** Creates a new instance of pluginLoader */
046: public pluginLoader() {
047: }
048:
049: public static Object[] getPlugins(String pckgname, Class wanted)
050: throws ClassNotFoundException {
051: ArrayList plugins = new ArrayList();
052: // Get a File object for the package
053: File directory = null;
054: try {
055: ClassLoader cld = Thread.currentThread()
056: .getContextClassLoader();
057: if (cld == null) {
058: throw new ClassNotFoundException(
059: "Can't get class loader.");
060: }
061: String path = pckgname.replace('.', '/');
062: URL resource = cld.getResource(path);
063: if (resource == null) {
064: throw new ClassNotFoundException("No resource for "
065: + path);
066: }
067: directory = new File(resource.getFile());
068: } catch (NullPointerException x) {
069: throw new ClassNotFoundException(pckgname + " ("
070: + directory
071: + ") does not appear to be a valid package");
072: }
073: if (directory.exists()) {
074: // Get the list of the files contained in the package
075: String[] files = directory.list();
076: for (int i = 0; i < files.length; i++) {
077: // we are only interested in .class files
078: if (files[i].endsWith(".class")) {
079: // removes the .class extension
080: Class c = Class.forName(pckgname
081: + '.'
082: + files[i].substring(0,
083: files[i].length() - 6));
084: try {
085: Object plugin = c.newInstance();
086: if (wanted.isInstance(plugin)) {
087: plugins.add(plugin);
088: }
089: } catch (Exception e) {
090:
091: }
092: }
093: }
094: } else {
095: throw new ClassNotFoundException(pckgname
096: + " does not appear to be a valid package");
097: }
098: return plugins.toArray();
099: }
100:
101: }
|