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