Coverage Report - org.apache.myfaces.resource.ClassLoaderContractResourceLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassLoaderContractResourceLoader
0%
0/45
0%
0/28
3.25
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.resource;
 20  
 
 21  
 import java.io.InputStream;
 22  
 import java.net.URL;
 23  
 import org.apache.myfaces.shared.resource.ContractResourceLoader;
 24  
 import org.apache.myfaces.shared.resource.ResourceMeta;
 25  
 import org.apache.myfaces.shared.resource.ResourceMetaImpl;
 26  
 import org.apache.myfaces.shared.util.ClassUtils;
 27  
 
 28  
 /**
 29  
  *
 30  
  * @author Leonardo Uribe
 31  
  */
 32  
 public class ClassLoaderContractResourceLoader extends ContractResourceLoader
 33  
 {
 34  
 
 35  
     public ClassLoaderContractResourceLoader(String prefix)
 36  
     {
 37  0
         super(prefix);
 38  0
     }
 39  
 
 40  
     @Override
 41  
     public String getLibraryVersion(String path, String contractName)
 42  
     {
 43  0
         return null;
 44  
     }
 45  
 
 46  
     @Override
 47  
     public InputStream getResourceInputStream(ResourceMeta resourceMeta)
 48  
     {
 49  0
         InputStream is = null;
 50  0
         if (getPrefix() != null && !"".equals(getPrefix()))
 51  
         {
 52  0
             String name = getPrefix() + '/' + resourceMeta.getContractName() 
 53  
                 + '/' + resourceMeta.getResourceIdentifier();
 54  0
             is = getClassLoader().getResourceAsStream(name);
 55  0
             if (is == null)
 56  
             {
 57  0
                 is = this.getClass().getClassLoader().getResourceAsStream(name);
 58  
             }
 59  0
             return is;
 60  
         }
 61  
         else
 62  
         {
 63  0
             String name = resourceMeta.getContractName() 
 64  
                 + '/' + resourceMeta.getResourceIdentifier();
 65  0
             is = getClassLoader().getResourceAsStream(name);
 66  0
             if (is == null)
 67  
             {
 68  0
                 is = this.getClass().getClassLoader().getResourceAsStream(name);
 69  
             }
 70  0
             return is;
 71  
         }
 72  
     }
 73  
 
 74  
     @Override
 75  
     public URL getResourceURL(ResourceMeta resourceMeta)
 76  
     {
 77  0
         URL url = null;
 78  0
         if (getPrefix() != null && !"".equals(getPrefix()))
 79  
         {
 80  0
             String name = getPrefix() + '/' + resourceMeta.getContractName() + 
 81  
                 '/' + resourceMeta.getResourceIdentifier();
 82  0
             url = getClassLoader().getResource(name);
 83  0
             if (url == null)
 84  
             {
 85  0
                 url = this.getClass().getClassLoader().getResource(name);
 86  
             }
 87  0
             return url;
 88  
         }
 89  
         else
 90  
         {
 91  0
             String name = resourceMeta.getContractName() + '/' + resourceMeta.getResourceIdentifier();
 92  0
             url = getClassLoader().getResource(name);
 93  0
             if (url == null)
 94  
             {
 95  0
                 url = this.getClass().getClassLoader().getResource(name);
 96  
             }
 97  0
             return url;
 98  
         }
 99  
     }
 100  
 
 101  
     @Override
 102  
     public String getResourceVersion(String path, String contractName)
 103  
     {
 104  0
         return null;
 105  
     }
 106  
 
 107  
     @Override
 108  
     public ResourceMeta createResourceMeta(String prefix, String libraryName, String libraryVersion,
 109  
                                            String resourceName, String resourceVersion, 
 110  
                                            String contractName)
 111  
     {
 112  0
         return new ResourceMetaImpl(prefix, libraryName, libraryVersion, resourceName, 
 113  
             resourceVersion, contractName);
 114  
     }
 115  
 
 116  
     /**
 117  
      * Returns the ClassLoader to use when looking up resources under the top level package. By default, this is the
 118  
      * context class loader.
 119  
      * 
 120  
      * @return the ClassLoader used to lookup resources
 121  
      */
 122  
     protected ClassLoader getClassLoader()
 123  
     {
 124  0
         return ClassUtils.getContextClassLoader();
 125  
     }
 126  
 
 127  
     @Override
 128  
     public boolean libraryExists(String libraryName, String contractName)
 129  
     {
 130  0
         if (getPrefix() != null && !"".equals(getPrefix()))
 131  
         {
 132  0
             String name = getPrefix() + '/' + 
 133  
                 contractName + '/' + libraryName;
 134  0
             URL url = getClassLoader().getResource(name);
 135  0
             if (url == null)
 136  
             {
 137  0
                 url = this.getClass().getClassLoader().getResource(name);
 138  
             }
 139  0
             if (url != null)
 140  
             {
 141  0
                 return true;
 142  
             }
 143  0
         }
 144  
         else
 145  
         {
 146  0
             String name = contractName + '/' + libraryName;
 147  0
             URL url = getClassLoader().getResource(name);
 148  0
             if (url == null)
 149  
             {
 150  0
                 url = this.getClass().getClassLoader().getResource(name);
 151  
             }
 152  0
             if (url != null)
 153  
             {
 154  0
                 return true;
 155  
             }
 156  
         }
 157  0
         return false;
 158  
     }
 159  
 
 160  
 }