1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
38
39
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
48
49 public void setSearchPath(List<Store> searchPath)
50 {
51 this.searchPath = searchPath;
52 }
53
54
55
56
57 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
58 {
59 lifecycle.setApplicationContext(applicationContext);
60 }
61
62
63
64
65 public void onApplicationEvent(ApplicationEvent event)
66 {
67 lifecycle.onApplicationEvent(event);
68 }
69
70
71
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
92
93
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
110
111
112
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
129
130
131
132
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
149
150
151
152
153
154
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 }