View Javadoc

1   /* 
2    * Copyright 2002-2004 The Apache Software Foundation
3    * Licensed  under the  Apache License,  Version 2.0  (the "License");
4    * you may not use  this file  except in  compliance with the License.
5    * You may obtain a copy of the License at 
6    * 
7    *   http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed  under the  License is distributed on an "AS IS" BASIS,
11   * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
12   * implied.
13   * 
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.struts.flow.core.source.impl;
18  
19  import java.io.IOException;
20  import java.net.MalformedURLException;
21  import java.net.URL;
22  import java.util.Map;
23  
24  import org.apache.struts.flow.core.Factory;
25  import org.apache.struts.flow.core.Logger;
26  
27  import org.apache.struts.flow.core.source.Source;
28  import org.apache.struts.flow.core.source.SourceFactory;
29  
30  /***
31   * A factory for a {@link URL} wrapper
32   * 
33   * @avalon.component
34   * @avalon.service type=SourceFactory
35   * @x-avalon.info name=url-source
36   * @x-avalon.lifestyle type=singleton
37   *
38   * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
39   * @version $Id: URLSourceFactory.java,v 1.4 2004/02/28 11:47:24 cziegeler Exp $
40   */
41  public class URLSourceFactory implements SourceFactory
42  {
43  
44      /***
45       * Create an URL-based source. This class actually creates an {@link URLSource}, but if another
46       * implementation is needed, subclasses can override this method.
47       */
48      protected Source createURLSource(URL url, Map parameters) throws MalformedURLException, IOException
49      {
50          URLSource result = new URLSource();
51          result.init(url, parameters);
52          return result;
53      }
54  
55      /***
56       * Create an file-based source. This class actually creates an {@link FileSource}, but if another
57       * implementation is needed, subclasses can override this method.
58       */
59      protected Source createFileSource(String uri) throws MalformedURLException, IOException
60      {
61          return new FileSource(uri);
62      }
63  
64      /***
65       * @see org.apache.struts.flow.core.source.SourceFactory#getSource(java.lang.String, java.util.Map)
66       */
67      public Source getSource(String uri, Map parameters) throws MalformedURLException, IOException
68      {
69          if (getLogger().isDebugEnabled())
70          {
71              final String message = "Creating source object for " + uri;
72              getLogger().debug(message);
73          }
74  
75          // First check if it's a file
76          if (uri.startsWith("file:"))
77          {
78              // Yes : return a file source
79              return createFileSource(uri);
80          }
81          else
82          {
83              // Not a "file:" : create an URLSource
84              // First try to create the URL
85              URL url;
86              try
87              {
88                  url = new URL(uri);
89              }
90              catch (MalformedURLException mue)
91              {
92                  // Maybe a file name containing a ':' ?
93                  if (getLogger().isDebugEnabled())
94                  {
95                      this.getLogger().debug("URL " + uri + " is malformed. Assuming it's a file path.", mue);
96                  }
97                  return createFileSource(uri);
98              }
99  
100             return createURLSource(url, parameters);
101         }
102     }
103 
104     /***
105      * @see org.apache.struts.flow.core.source.SourceFactory#release(org.apache.struts.flow.core.source.Source)
106      */
107     public void release(Source source)
108     {
109         // do nothing here
110     }
111     
112     public Logger getLogger() {
113         return Factory.getLogger();
114     }
115 }