View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.springframework.beans.BeansException;
29  import org.springframework.context.ApplicationContext;
30  import org.springframework.context.ApplicationContextAware;
31  import org.springframework.context.ApplicationEvent;
32  import org.springframework.context.ApplicationListener;
33  import org.springframework.extensions.surf.util.AbstractLifecycleBean;
34  
35  
36  /**
37   * Web Script Storage
38   * 
39   * @author davidc
40   */
41  public class SearchPath implements ApplicationContextAware, ApplicationListener
42  {
43      private ProcessorLifecycle lifecycle = new ProcessorLifecycle();
44      private Collection<Store> searchPath = Collections.emptyList();
45  
46      /**
47       * @param searchPath
48       */
49      public void setSearchPath(List<Store> searchPath)
50      {
51          this.searchPath = searchPath;
52      }
53      
54      /* (non-Javadoc)
55       * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
56       */
57      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
58      {
59          lifecycle.setApplicationContext(applicationContext);
60      }
61  
62      /* (non-Javadoc)
63       * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
64       */
65      public void onApplicationEvent(ApplicationEvent event)
66      {
67          lifecycle.onApplicationEvent(event);
68      }
69      
70      /**
71       * Hooks into Spring Application Lifecycle
72       */
73      private class ProcessorLifecycle extends AbstractLifecycleBean
74      {
75          @Override
76          protected void onBootstrap(ApplicationEvent event)
77          {
78              for (Store store : searchPath)
79              {
80                  store.init();
81              }
82          }
83      
84          @Override
85          protected void onShutdown(ApplicationEvent event)
86          {
87          }
88      }
89  
90      /**
91       * Gets all Web Script Stores
92       * 
93       * @return  all Web Script Stores
94       */
95      public Collection<Store> getStores()
96      {
97          Collection<Store> aliveStores = new ArrayList<Store>(searchPath.size());
98          for (Store store : searchPath)
99          {
100             if (store.exists())
101             {
102                 aliveStores.add(store);
103             }
104         }
105         return aliveStores;
106     }
107 
108     /**
109      * Gets the Web Script Store for the given Store path
110      * 
111      * @param storePath
112      * @return  store (or null, if not found)
113      */
114     public Store getStore(String storePath)
115     {
116         Collection<Store> stores = getStores();
117         for (Store store : stores)
118         {
119             if (store.getBasePath().equals(storePath))
120             {
121                 return store;
122             }
123         }
124         return null;
125     }
126     
127     /**
128      * Determines if the document exists anywhere on the search path
129      * 
130      * @param documentPath  document path
131      * @return  true => exists, false => does not exist
132      * @throws IOException 
133      */
134     public boolean hasDocument(String documentPath) throws IOException
135     {
136        for (Store store : getStores())
137        {
138            if (store.hasDocument(documentPath))
139            {
140                return true;
141            }
142        }
143        
144        return false;
145     }
146 
147     /**
148      * Gets a document from anywhere on the search path
149      * 
150      * @param documentPath  document path
151      * @return input stream onto document or null if it
152      *         does not exist on the search path
153      * 
154      * @throws IOException
155      */
156     public InputStream getDocument(String documentPath) throws IOException
157     {
158        for (Store store : getStores())
159        {
160            if (store.hasDocument(documentPath))
161            {
162                return store.getDocument(documentPath);
163            }
164        }
165        
166        return null;
167     }
168 }