Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » server » axis2 » util » [javadoc | source]
    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.openejb.server.axis2.util;
   20   
   21   import java.io.File;
   22   import java.io.IOException;
   23   import java.io.InputStream;
   24   import java.net.URI;
   25   import java.net.URISyntaxException;
   26   import java.net.URL;
   27   
   28   public class SimpleUriResolver {
   29       private URI uri;
   30       private URL url;
   31       private InputStream is;
   32   
   33       public SimpleUriResolver() {
   34       }
   35   
   36       public SimpleUriResolver(String path) throws IOException {
   37           this("", path);
   38       }
   39   
   40       public SimpleUriResolver(String baseUriStr, String uriStr) throws IOException {
   41           if (baseUriStr != null && baseUriStr.startsWith("jar:")) {
   42               tryJar(baseUriStr, uriStr);
   43           } else if (uriStr.startsWith("jar:")) {
   44               tryJar(uriStr);
   45           } else {
   46               tryFileSystem(baseUriStr, uriStr);
   47           }
   48       }
   49   
   50       public void resolve(String baseUriStr, String uriStr) throws IOException {
   51           this.uri = null;
   52           this.url = null;
   53           this.is = null;
   54   
   55           if (baseUriStr != null && baseUriStr.startsWith("jar:")) {
   56               tryJar(baseUriStr, uriStr);
   57           } else if (uriStr.startsWith("jar:")) {
   58               tryJar(uriStr);
   59           } else {
   60               tryFileSystem(baseUriStr, uriStr);
   61           }
   62       }
   63   
   64       private void tryFileSystem(String baseUriStr, String uriStr) throws IOException {
   65           try {
   66               URI relative;
   67               File uriFile = new File(uriStr);
   68               uriFile = new File(uriFile.getAbsolutePath());
   69   
   70               if (uriFile.exists()) {
   71                   relative = uriFile.toURI();
   72               } else {
   73                   relative = new URI(uriStr.replaceAll(" ", "%20"));
   74               }
   75   
   76               if (relative.isAbsolute()) {
   77                   uri = relative;
   78                   url = relative.toURL();
   79                   is = url.openStream();
   80               } else if (baseUriStr != null) {
   81                   URI base;
   82                   File baseFile = new File(baseUriStr);
   83   
   84                   if (!baseFile.exists() && baseUriStr.startsWith("file:/")) {
   85                       baseFile = new File(baseUriStr.substring(6));
   86                   }
   87   
   88                   if (baseFile.exists()) {
   89                       base = baseFile.toURI();
   90                   } else {
   91                       base = new URI(baseUriStr);
   92                   }
   93   
   94                   base = base.resolve(relative);
   95                   if (base.isAbsolute()) {
   96                       uri = base;
   97                       url = base.toURL();
   98                       is = url.openStream();
   99                   }
  100               }
  101           } catch (URISyntaxException e) {
  102               // do nothing
  103           }
  104       }
  105   
  106       private void tryJar(String baseStr, String uriStr) throws IOException {
  107           int i = baseStr.indexOf('!');
  108           if (i == -1) {
  109               tryFileSystem(baseStr, uriStr);
  110           }
  111   
  112           String jarBase = baseStr.substring(0, i + 1);
  113           String jarEntry = baseStr.substring(i + 1);
  114           try {
  115               URI u = new URI(jarEntry).resolve(uriStr);
  116   
  117               tryJar(jarBase + u.toString());
  118   
  119               if (is != null) {
  120                   if (u.isAbsolute()) {
  121                       url = u.toURL();
  122                   }
  123                   return;
  124               }
  125           } catch (URISyntaxException e) {
  126               // do nothing
  127           }
  128   
  129           tryFileSystem("", uriStr);
  130       }
  131   
  132       private void tryJar(String uriStr) throws IOException {
  133           int i = uriStr.indexOf('!');
  134           if (i == -1) {
  135               return;
  136           }
  137   
  138           url = new URL(uriStr);
  139           try {
  140               is = url.openStream();
  141               try {
  142                   uri = url.toURI();
  143               } catch (URISyntaxException ex) {
  144                   // ignore
  145               }
  146           } catch (IOException e) {
  147               // do nothing
  148           }
  149       }
  150   
  151       public URI getURI() {
  152           return uri;
  153       }
  154   
  155       public URL getURL() {
  156           return url;
  157       }
  158   
  159       public InputStream getInputStream() {
  160           return is;
  161       }
  162   
  163       public boolean isResolved() {
  164           return is != null;
  165       }
  166   }
  167   	

Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » server » axis2 » util » [javadoc | source]