Coverage Report - org.kuali.rice.kew.plugin.PluginUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
PluginUtils
0%
0/72
0%
0/40
4.308
PluginUtils$1
0%
0/2
N/A
4.308
PluginUtils$2
0%
0/2
0%
0/4
4.308
PluginUtils$PluginZipFileFilter
0%
0/2
0%
0/4
4.308
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.plugin;
 18  
 
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.apache.log4j.Logger;
 21  
 import org.kuali.rice.core.api.resourceloader.ResourceLoader;
 22  
 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 23  
 import org.kuali.rice.core.framework.resourceloader.BaseResourceLoader;
 24  
 import org.kuali.rice.core.impl.resourceloader.ResourceLoaderUtil;
 25  
 import org.kuali.rice.kew.resourceloader.CoreResourceLoader;
 26  
 
 27  
 import javax.xml.namespace.QName;
 28  
 import java.io.File;
 29  
 import java.io.FileFilter;
 30  
 import java.io.FilenameFilter;
 31  
 import java.util.Iterator;
 32  
 import java.util.List;
 33  
 
 34  
 
 35  
 /**
 36  
  * Various plugin utilities.
 37  
  *
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 39  
  */
 40  
 public final class PluginUtils {
 41  
 
 42  0
     private static final Logger LOG = Logger.getLogger(PluginUtils.class);
 43  
     private static final String SHARED_DIR = "shared";
 44  
 
 45  
     // maximum time we should wait for a new plugin directory to stop being
 46  
     // modified before we give up on loading it this time around
 47  
     public static final long INFINITE_MAX_WAIT = -1;
 48  
     // NOTE: must be greater than the SAFE TIME
 49  
     public static final long DEFAULT_MAX_WAIT = 90000;
 50  
 
 51  
     // amount of time since the last time the plugin dir was updated that we
 52  
     // consider "safe" to proceed with registering the plugin
 53  
     // basically, with the current implementation, an amount of time that we
 54  
     // expect any files in process of modification to complete (e.g. copy)
 55  
     // NOTE: MUST be LESS than the MAX WAIT otherwise, we will ALWAYS fail to wait
 56  
     public static final long DEFAULT_SAFE_TIME = 60000;
 57  
 
 58  0
     private static final FilenameFilter JAR_FILES_FILTER = new FilenameFilter() {
 59  
         public boolean accept(File dir, String name) {
 60  0
             return name.matches(".+\\.jar");
 61  
         }
 62  
     };
 63  
 
 64  0
     private static final FileFilter SHARED_DIR_FILTER = new FileFilter() {
 65  
         public boolean accept(File file) {
 66  0
             return file.isDirectory() && file.getName().equals(SHARED_DIR);
 67  
         }
 68  
     };
 69  
     
 70  0
         private PluginUtils() {
 71  0
                 throw new UnsupportedOperationException("do not call");
 72  
         }
 73  
 
 74  0
     public static class PluginZipFileFilter implements FileFilter {
 75  
         public boolean accept(File file) {
 76  0
             return file.isFile() && file.getName().endsWith(".zip");
 77  
         }
 78  
     }
 79  
 
 80  
     public static String getLogPrefix(Plugin plugin) {
 81  0
             return getLogPrefix(plugin.getName());
 82  
     }
 83  
 
 84  
     public static String getLogPrefix(QName pluginName) {
 85  0
             return "[Plugin: " + pluginName + "]";
 86  
     }
 87  
 
 88  
     static File[] findJars(File libDir) {
 89  0
         File[] jarFiles = new File[0];
 90  0
         if (libDir.isDirectory()) {
 91  0
             jarFiles = libDir.listFiles(JAR_FILES_FILTER);
 92  
         }
 93  0
         return jarFiles;
 94  
     }
 95  
 
 96  
     public static File findSharedDirectory(List pluginDirectories) {
 97  0
         for (Iterator iterator = pluginDirectories.iterator(); iterator.hasNext();) {
 98  0
             File dir = new File((String) iterator.next());
 99  0
             if (dir.isDirectory()) {
 100  0
                 File[] subDirs = dir.listFiles(SHARED_DIR_FILTER);
 101  0
                 if (subDirs.length > 0) {
 102  0
                     return subDirs[0];
 103  
                 }
 104  
             }
 105  0
         }
 106  0
         return null;
 107  
     }
 108  
 
 109  
     public static void validatePluginZipFile(File file) {
 110  0
             if (file == null) {
 111  0
                         throw new IllegalArgumentException("Given plugin file was 'null'");
 112  0
                 } else if (!file.exists()) {
 113  0
                         throw new IllegalArgumentException("Given plugin file does not exist: " + file.getAbsolutePath());
 114  0
                 } else if (!file.isFile()) {
 115  0
                         throw new IllegalArgumentException("Given plugin file is not a valid file: " + file.getAbsolutePath());
 116  0
                 } else if (!file.canRead()) {
 117  0
                         throw new IllegalArgumentException("Permission denied to read given plugin file: " + file.getAbsolutePath());
 118  0
                 } else if (!file.getName().endsWith(".zip")) {
 119  0
                         throw new IllegalArgumentException("Given plugin file does not end in .zip extension: " + file.getAbsolutePath());
 120  
                 }
 121  
             // now look at the directory the plugin file is in because we need to be able to extract it there
 122  0
             File pluginDirectory = file.getParentFile();
 123  0
             validatePluginDirectory(pluginDirectory);
 124  
             // also verify that we can write to this directory
 125  0
             if (pluginDirectory == null) {
 126  0
                     throw new IllegalArgumentException("Given plugin directory was 'null'");
 127  0
             } else if (!pluginDirectory.canWrite()) {
 128  0
                     throw new IllegalArgumentException("Impossible to write to plugin directory so plugin cannot be expanded: " + pluginDirectory.getAbsolutePath());
 129  
             }
 130  0
     }
 131  
 
 132  
     public static void validatePluginDirectory(File directory) {
 133  0
             if (directory == null) {
 134  0
                         throw new IllegalArgumentException("Given directory was 'null'");
 135  0
                 } else if (!directory.exists()) {
 136  0
                         throw new IllegalArgumentException("Given directory does not exist: " + directory.getAbsolutePath());
 137  0
                 } else if (!directory.isDirectory()) {
 138  0
                         throw new IllegalArgumentException("Given plugin directory is not a valid directory: " + directory.getAbsolutePath());
 139  0
                 } else if (!directory.canRead()) {
 140  0
                         throw new IllegalArgumentException("Permission denied to read given plugin directory: " + directory.getAbsolutePath());
 141  
                 }
 142  0
     }
 143  
 
 144  
     public static PluginRegistry getPluginRegistry() {
 145  0
             return ((CoreResourceLoader)GlobalResourceLoader.getResourceLoader(CoreResourceLoader.NAME)).getRegistry();
 146  
     }
 147  
 
 148  
     public static void installResourceLoader(Plugin plugin) {
 149  0
             if (plugin.getConfig() instanceof PluginConfig) {
 150  0
                         PluginConfig pluginConfig = (PluginConfig)plugin.getConfig();
 151  0
                         if (!StringUtils.isEmpty(pluginConfig.getResourceLoaderClassname())) {
 152  0
                 ResourceLoader resourceLoader = (ResourceLoader) ResourceLoaderUtil.createObject(pluginConfig.getResourceLoaderClassname(), plugin.getClassLoader());
 153  0
                 if (resourceLoader == null) {
 154  0
                     LOG.warn("Could not create resource loader from plugin resource loader class: " + pluginConfig.getResourceLoaderClassname());
 155  
                     // if null, use a default resource loader
 156  0
                                         resourceLoader = new BaseResourceLoader(plugin.getName());
 157  
                 }
 158  0
                                 plugin.addResourceLoader(resourceLoader);
 159  
                         }
 160  
                 }
 161  0
     }
 162  
 
 163  
     public static void installPluginListeners(Plugin plugin) {
 164  0
             if (plugin.getConfig() instanceof PluginConfig) {
 165  0
                         PluginConfig pluginConfig = (PluginConfig)plugin.getConfig();
 166  0
                         for (Iterator iterator = pluginConfig.getListeners().iterator(); iterator.hasNext();) {
 167  0
                     String pluginListenerClassName = (String) iterator.next();
 168  
                     try {
 169  0
                         Class listenerClass = Class.forName(pluginListenerClassName, true, plugin.getClassLoader());
 170  0
                         plugin.addPluginListener((PluginListener)listenerClass.newInstance());
 171  0
                     } catch (ClassNotFoundException e) {
 172  0
                         throw new PluginException(getLogPrefix(plugin)+" Error finding listener class '"+pluginListenerClassName+"'.", e);
 173  0
                     } catch (InstantiationException e) {
 174  0
                         throw new PluginException(getLogPrefix(plugin)+" Error creating an instance of listener class '"+pluginListenerClassName+"'.", e);
 175  0
                     } catch (IllegalAccessException e) {
 176  0
                         throw new PluginException(getLogPrefix(plugin)+" Error creating an instance of listener class '"+pluginListenerClassName+"'.", e);
 177  0
                     } catch (ClassCastException e) {
 178  0
                         throw new PluginException(getLogPrefix(plugin)+" Listener class '"+pluginListenerClassName+"' does not implement PluginListener.", e);
 179  0
                     }
 180  0
                 }
 181  
                 }
 182  0
     }
 183  
 }