001: package com.sun.portal.app.filesharing.repo;
002:
003: import com.sun.portal.app.filesharing.servlet.FileDownloadServlet;
004: import com.sun.portal.app.filesharing.util.InfoResolver;
005: import com.sun.portal.app.filesharing.util.InfoResolverFactory;
006: import com.sun.portal.app.filesharing.util.ParameterCheck;
007:
008: import javax.faces.context.ExternalContext;
009: import javax.faces.context.FacesContext;
010: import javax.portlet.PortletContext;
011: import javax.servlet.ServletContext;
012: import java.io.File;
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.util.Collections;
016: import java.util.Enumeration;
017: import com.sun.portal.search.demo.SearchDatabase;
018:
019: /**
020: * @author Alejandro Abdelnur
021: */
022: public class RepoFactory {
023:
024: public static final String REPOSITORY_TYPE = "filesharing.Repository.type";
025:
026: public static final String FILE_REPOSITORY_ROOT = "filesharing.FileRepository.root";
027:
028: public static final String JDBC_REPOSITORY_DATASOURCE = "filesharing.JdbcRepository.datasource";
029: public static final String REPO_OWNER_PROXY = "__repo_owner_proxy__";
030:
031: public static Repository getRepository(PortletContext ctx,
032: String repositoryId, String uid) throws RepoException {
033: ParameterCheck.checkNotNull("ctx", ctx);
034: ParameterCheck
035: .checkNotEmpty("repositoryId", repositoryId, true);
036: return getRepository(ctx.getInitParameter(REPOSITORY_TYPE),
037: new InitParameters(ctx), repositoryId, uid);
038: }
039:
040: public static Repository getRepository(ServletContext ctx,
041: String repositoryId, String uid) throws RepoException {
042: ParameterCheck.checkNotNull("ctx", ctx);
043: ParameterCheck
044: .checkNotEmpty("repositoryId", repositoryId, true);
045: return getRepository(ctx.getInitParameter(REPOSITORY_TYPE),
046: new InitParameters(ctx), repositoryId, uid);
047: }
048:
049: public static Repository getRepository(FacesContext ctx,
050: String repositoryId, String uid) throws RepoException {
051: ParameterCheck.checkNotNull("ctx", ctx);
052: ParameterCheck
053: .checkNotEmpty("repositoryId", repositoryId, true);
054: ExternalContext eCtx = ctx.getExternalContext();
055: InfoResolver info = InfoResolverFactory.getFactory()
056: .getInfoResolver(ctx);
057: String searchUrl = info.getSearchServerURL();
058: String dbName = info.getSearchDatabase();
059: boolean isOwner = info.isRepositoryOwner();
060: SearchDatabase searchDb = null;
061: try {
062: if (searchUrl != null && dbName != null) {
063: searchDb = new SearchDatabase(searchUrl, dbName);
064: searchDb.setSessionID(info.getSessionId());
065: }
066: } catch (Exception dbe) {
067: }
068: Repository repo = getRepository(eCtx
069: .getInitParameter(REPOSITORY_TYPE), new InitParameters(
070: eCtx), repositoryId, uid, searchDb, isOwner);
071: if (repo instanceof IndexedRepository) {
072: String urlPattern = ctx.getExternalContext()
073: .getRequestContextPath()
074: + "/file/{0}?"
075: + FileDownloadServlet.FILE_PARAM
076: + "={1}&"
077: + InfoResolverFactory.REPOSITORY_ID_PARAM
078: + "=" + repositoryId;
079: ((IndexedRepository) repo).setUrlPattern(urlPattern);
080: }
081: return repo;
082: }
083:
084: private static Repository getRepository(String repositoryType,
085: InitParameters initParams, String repositoryId, String uid)
086: throws RepoException {
087: return getRepository(repositoryType, initParams, repositoryId,
088: uid, null, false);
089: }
090:
091: private static Repository getRepository(String repositoryType,
092: InitParameters initParams, String repositoryId, String uid,
093: SearchDatabase searchDb, boolean isOwner)
094: throws RepoException {
095: Repository repo;
096: if (repositoryType.equals("FileSystem")) {
097: String repoRoot = initParams
098: .getInitParameter(FILE_REPOSITORY_ROOT);
099: ParameterCheck.checkNotEmpty(
100: "FileSystem Repository init parameter ["
101: + FILE_REPOSITORY_ROOT + "]", repoRoot,
102: true);
103: repo = new FileRepository(new File(repoRoot), repositoryId);
104: } else if (repositoryType.equals("Database")) {
105: String repoDatasource = initParams
106: .getInitParameter(JDBC_REPOSITORY_DATASOURCE);
107: ParameterCheck.checkNotEmpty(
108: "Database Repository init parameter ["
109: + JDBC_REPOSITORY_DATASOURCE + "]",
110: repoDatasource, true);
111: repo = new JdbcRepository(repoDatasource, repositoryId, uid);
112: if (isOwner)
113: ((JdbcRepository) repo).setIsOnwer(isOwner);
114: } else {
115: throw new IllegalArgumentException(
116: "Invalid Repository type [" + repositoryType + "]");
117: }
118: repo = new ParamCheckerFacadeRepository(repo);
119: if (searchDb != null) {
120: try {
121: repo = new IndexedRepository(repo, repositoryId,
122: searchDb);
123: } catch (Exception e) {
124:
125: }
126: }
127: return repo;
128: }
129:
130: private static class InitParameters {
131: private PortletContext _pCtx;
132: private ServletContext _sCtx;
133: private ExternalContext _fCtx;
134:
135: public InitParameters(PortletContext ctx) {
136: _pCtx = ctx;
137: }
138:
139: public InitParameters(ServletContext ctx) {
140: _sCtx = ctx;
141: }
142:
143: public InitParameters(ExternalContext ctx) {
144: _fCtx = ctx;
145: }
146:
147: public Enumeration getInitParameterNames() {
148: Enumeration enum1;
149: if (_fCtx != null) {
150: enum1 = Collections.enumeration(_fCtx
151: .getInitParameterMap().keySet());
152: } else if (_sCtx != null) {
153: enum1 = _sCtx.getInitParameterNames();
154: } else {
155: enum1 = _pCtx.getInitParameterNames();
156: }
157: return enum1;
158: }
159:
160: public String getInitParameter(String name) {
161: String initParam;
162: if (_fCtx != null) {
163: initParam = _fCtx.getInitParameter(name);
164: } else if (_sCtx != null) {
165: initParam = _sCtx.getInitParameter(name);
166: } else {
167: initParam = _pCtx.getInitParameter(name);
168: }
169: return initParam;
170: }
171: }
172:
173: private static class ParamCheckerFacadeRepository implements
174: Repository {
175: private Repository _repo;
176:
177: public ParamCheckerFacadeRepository(Repository repo) {
178: _repo = repo;
179: }
180:
181: public void addFile(RepoItem file, InputStream is)
182: throws RepoException, IOException {
183: ParameterCheck.checkNotNull("file", file);
184: ParameterCheck.checkNotNull("is", is);
185: _repo.addFile(file, is);
186: }
187:
188: public void replaceFile(RepoItem file, InputStream is)
189: throws RepoException, IOException {
190: ParameterCheck.checkNotNull("file", file);
191: ParameterCheck.checkNotNull("is", is);
192: _repo.replaceFile(file, is);
193: }
194:
195: public InputStream getFile(RepoItem file) throws RepoException,
196: IOException {
197: ParameterCheck.checkNotNull("file", file);
198: return _repo.getFile(file);
199: }
200:
201: public void deleteFile(RepoItem file) throws RepoException {
202: ParameterCheck.checkNotNull("file", file);
203: _repo.deleteFile(file);
204: }
205:
206: public void addFolder(RepoItem folder) throws RepoException {
207: ParameterCheck.checkNotNull("folder", folder);
208: _repo.addFolder(folder);
209: }
210:
211: public RepoItem[] getContent(RepoItem folder)
212: throws RepoException {
213: ParameterCheck.checkNotNull("folder", folder);
214: return _repo.getContent(folder);
215: }
216:
217: public RepoItem[] getContent(RepoItem folder, String filter)
218: throws RepoException {
219: ParameterCheck.checkNotNull("folder", folder);
220: ParameterCheck.checkNotNull("filter", filter);
221: return _repo.getContent(folder, filter);
222: }
223:
224: public void deleteFolder(RepoItem folder, boolean deleteNotEmpty)
225: throws RepoException {
226: ParameterCheck.checkNotNull("folder", folder);
227: _repo.deleteFolder(folder, deleteNotEmpty);
228: }
229:
230: public void copy(RepoItem item, RepoItem folder)
231: throws RepoException {
232: ParameterCheck.checkNotNull("item", item);
233: ParameterCheck.checkNotNull("folder", folder);
234: _repo.copy(item, folder);
235: }
236:
237: public void move(RepoItem item, RepoItem folder)
238: throws RepoException {
239: ParameterCheck.checkNotNull("item", item);
240: ParameterCheck.checkNotNull("folder", folder);
241: _repo.move(item, folder);
242: }
243:
244: public void rename(RepoItem item, String newName)
245: throws RepoException {
246: ParameterCheck.checkNotNull("item", item);
247: ParameterCheck.checkNotEmpty("newName", newName, true);
248: new RepoItem((item.getParent() != null) ? item.getParent()
249: : RepoItem.ROOT, newName, "dummy");
250: _repo.rename(item, newName);
251: }
252:
253: public RepoItem getItemInfo(RepoItem item) throws RepoException {
254: ParameterCheck.checkNotNull("item", item);
255: return _repo.getItemInfo(item);
256: }
257: }
258: }
|