Coverage Report - org.argouml.profile.init.ProfileLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
ProfileLoader
37%
27/72
37%
9/24
4.333
ProfileLoader$JarFileFilter
100%
2/2
50%
3/6
4.333
 
 1  
 /* $Id: ProfileLoader.java 17940 2010-01-30 16:15:11Z euluis $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009-2010 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    tfmorris
 11  
  *    euluis
 12  
  *****************************************************************************
 13  
  *
 14  
  * Some portions of this file was previously release using the BSD License:
 15  
  */
 16  
 
 17  
 // Copyright (c) 2008 The Regents of the University of California. All
 18  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 19  
 // software and its documentation without fee, and without a written
 20  
 // agreement is hereby granted, provided that the above copyright notice
 21  
 // and this paragraph appear in all copies. This software program and
 22  
 // documentation are copyrighted by The Regents of the University of
 23  
 // California. The software program and documentation are supplied "AS
 24  
 // IS", without any accompanying services from The Regents. The Regents
 25  
 // does not warrant that the operation of the program will be
 26  
 // uninterrupted or error-free. The end-user understands that the program
 27  
 // was developed for research purposes and is advised not to rely
 28  
 // exclusively on the program for any reason. IN NO EVENT SHALL THE
 29  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 30  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 31  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 32  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 33  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 34  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 35  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 36  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 37  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 38  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 39  
 
 40  
 package org.argouml.profile.init;
 41  
 
 42  
 import java.io.File;
 43  
 import java.io.FileFilter;
 44  
 import java.io.IOException;
 45  
 import java.net.URL;
 46  
 import java.net.URLClassLoader;
 47  
 import java.util.HashSet;
 48  
 import java.util.List;
 49  
 import java.util.Map;
 50  
 import java.util.Set;
 51  
 import java.util.StringTokenizer;
 52  
 import java.util.jar.Attributes;
 53  
 import java.util.jar.JarFile;
 54  
 import java.util.jar.Manifest;
 55  
 
 56  
 import org.apache.log4j.Logger;
 57  
 import org.argouml.cognitive.Critic;
 58  
 import org.argouml.i18n.Translator;
 59  
 import org.argouml.moduleloader.ModuleLoader2;
 60  
 import org.argouml.profile.ProfileException;
 61  
 import org.argouml.profile.ProfileFacade;
 62  
 import org.argouml.profile.UserDefinedProfile;
 63  
 
 64  
 /**
 65  
  * This is the profile loader that loads modules profiles
 66  
  * 
 67  
  * @author maurelio1234
 68  
  */
 69  900
 public final class ProfileLoader {
 70  
     /**
 71  
      * Logger.
 72  
      */
 73  900
     private static final Logger LOG = Logger.getLogger(ProfileLoader.class);
 74  
     
 75  
     /**
 76  
      * The prefix in URL:s that are jars.
 77  
      */
 78  
     private static final String JAR_PREFIX = "jar:";
 79  
     
 80  
     /**
 81  
      * The prefix in URL:s that are files.
 82  
      */
 83  
     private static final String FILE_PREFIX = "file:";
 84  
     
 85  
     /**
 86  
      * Looks for profiles in the jars in the directories used by the 
 87  
      * ModuleLoader to load modules
 88  
      */
 89  
     public void doLoad() {
 90  900
         List<String> extDirs = 
 91  
             ModuleLoader2.getInstance().getExtensionLocations();
 92  
         
 93  900
         for (String extDir : extDirs) {
 94  900
             huntForProfilesInDir(extDir);
 95  
         }
 96  900
     }
 97  
 
 98  
     private void huntForProfilesInDir(String dir) {
 99  900
         LOG.info("Looking for Profiles in " + dir);
 100  
         
 101  900
         File extensionDir = new File(dir);
 102  900
         if (extensionDir.isDirectory()) {
 103  900
             File[] files = extensionDir.listFiles(new JarFileFilter());
 104  5400
             for (File file : files) {
 105  4500
                 JarFile jarfile = null;
 106  
                 try {
 107  4500
                     jarfile = new JarFile(file);
 108  4500
                     if (jarfile != null) {
 109  4500
                         LOG.info("Looking for Profiles in the Jar "
 110  
                                 + jarfile.getName());
 111  
 
 112  4500
                         ClassLoader classloader = new URLClassLoader(
 113  
                                 new URL[] {file.toURI().toURL()});
 114  4500
                         loadProfilesFromJarFile(jarfile.getManifest(), file,
 115  
                                 classloader);
 116  
                     }
 117  0
                 } catch (IOException ioe) {
 118  0
                     LOG.debug("Cannot open Jar file " + file, ioe);
 119  4500
                 }
 120  
             }
 121  
         }
 122  
 
 123  900
     }
 124  
 
 125  
     /**
 126  
      * Interprets the MANIFEST file in the JAR in order to load the declared
 127  
      * profile.
 128  
      * 
 129  
      * @param file the file object referencing the Jar
 130  
      * @param manifest the manifest file of the Jar
 131  
      * @param classloader the classloader that loads the classes referenced by
 132  
      *            the Jar
 133  
      */
 134  
     private void loadProfilesFromJarFile(Manifest manifest, File file,
 135  
             ClassLoader classloader) {
 136  4500
         Map<String, Attributes> entries = manifest.getEntries();
 137  4500
         boolean classLoaderAlreadyAdded = false;
 138  
 
 139  4500
         for (String entryName : entries.keySet()) {
 140  4500
             Attributes attr = entries.get(entryName);
 141  4500
             if (new Boolean(attr.getValue("Profile") + "").booleanValue()) {
 142  
                 try {
 143  
                     // we only need to add the classloader once
 144  
                     // and if and only if there is at least a profile
 145  
                     // in the JAR
 146  0
                     if (!classLoaderAlreadyAdded) {
 147  0
                         Translator.addClassLoader(classloader);
 148  0
                         classLoaderAlreadyAdded = true;
 149  
                     }
 150  0
                     Set<Critic> critics = loadJavaCriticsForProfile(attr,
 151  
                             classloader);
 152  0
                     String modelPath = attr.getValue("Model");
 153  0
                     URL modelURL = null;
 154  
 
 155  0
                     if (modelPath != null) {
 156  0
                         modelURL = new URL(JAR_PREFIX + FILE_PREFIX
 157  
                                 + file.getCanonicalPath() + "!" + modelPath);
 158  
                     }
 159  
 
 160  0
                     UserDefinedProfile udp = new UserDefinedProfile(entryName,
 161  
                             modelURL, critics,
 162  
                             loadManifestDependenciesForProfile(attr),
 163  
                             ProfileFacade.getManager());
 164  
 
 165  0
                     ProfileFacade.getManager().registerProfile(udp);
 166  0
                     LOG.debug("Registered Profile: " + udp.getDisplayName()
 167  
                             + "...");
 168  0
                 } catch (ProfileException e) {
 169  0
                     LOG.error("Exception", e);
 170  0
                 } catch (IOException e) {
 171  0
                     LOG.error("Exception", e);
 172  0
                 }
 173  
             }
 174  
 
 175  4500
         }
 176  4500
     }
 177  
 
 178  
     /**
 179  
      * Resolves the dependencies for a Profile
 180  
      * 
 181  
      * @param attr a group of attributes in the MANIFEST file for this JAR
 182  
      * 
 183  
      * @return the set of defined profiles
 184  
      */
 185  
     private Set<String> loadManifestDependenciesForProfile(Attributes attr) {
 186  0
         Set<String> ret = new HashSet<String>();
 187  0
         String value = attr.getValue("Depends-on");
 188  0
         if (value != null) {
 189  0
             StringTokenizer st = new StringTokenizer(value, ",");
 190  
 
 191  0
             while (st.hasMoreElements()) {
 192  0
                 String entry = st.nextToken().trim();
 193  0
                 ret.add(entry);
 194  0
             }
 195  
         }
 196  
 
 197  0
         return ret;
 198  
     }
 199  
 
 200  
     /**
 201  
      * Loads the Java critics defined by a profile
 202  
      * 
 203  
      * @param attr the Manifest section of the profile
 204  
      * @param classloader the classloader of the Jar
 205  
      * 
 206  
      * @return the set of defined critics
 207  
      */
 208  
     private Set<Critic> loadJavaCriticsForProfile(Attributes attr,
 209  
             ClassLoader classloader) {
 210  0
         Set<Critic> ret = new HashSet<Critic>();
 211  
 
 212  0
         String value = attr.getValue("Java-Critics");
 213  0
         if (value != null) {
 214  0
             StringTokenizer st = new StringTokenizer(value, ",");
 215  
 
 216  0
             while (st.hasMoreElements()) {
 217  0
                 String entry = st.nextToken().trim();
 218  
 
 219  
                 try {
 220  0
                     Class cl = classloader.loadClass(entry);
 221  0
                     Critic critic = (Critic) cl.newInstance();
 222  0
                     ret.add(critic);
 223  0
                 } catch (ClassNotFoundException e) {
 224  0
                     LOG.error("Error loading class: " + entry, e);
 225  0
                 } catch (InstantiationException e) {
 226  0
                     LOG.error("Error instantianting class: " + entry, e);
 227  0
                 } catch (IllegalAccessException e) {
 228  0
                     LOG.error("Exception", e);
 229  0
                 }
 230  0
             }
 231  
         }
 232  
 
 233  0
         return ret;
 234  
     }
 235  
     
 236  
     /**
 237  
      * The file filter that selects Jar files.
 238  
      */
 239  900
     static class JarFileFilter implements FileFilter {
 240  
         /*
 241  
          * @see java.io.FileFilter#accept(java.io.File)
 242  
          */
 243  
         public boolean accept(File pathname) {
 244  4500
             return (pathname.canRead()
 245  
                     && pathname.isFile()
 246  
                     && pathname.getPath().toLowerCase().endsWith(".jar"));
 247  
         }
 248  
     }
 249  
     
 250  
 }