001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.documentlibrary.util;
022:
023: import com.liferay.documentlibrary.DuplicateDirectoryException;
024: import com.liferay.documentlibrary.DuplicateFileException;
025: import com.liferay.documentlibrary.NoSuchDirectoryException;
026: import com.liferay.documentlibrary.NoSuchFileException;
027: import com.liferay.portal.PortalException;
028: import com.liferay.portal.SystemException;
029: import com.liferay.portal.kernel.search.SearchException;
030: import com.liferay.portal.kernel.util.GetterUtil;
031: import com.liferay.portal.kernel.util.StringPool;
032: import com.liferay.portal.lucene.LuceneUtil;
033: import com.liferay.portal.util.PropsUtil;
034: import com.liferay.util.FileUtil;
035:
036: import java.io.File;
037: import java.io.FileInputStream;
038: import java.io.IOException;
039: import java.io.InputStream;
040:
041: import java.util.Arrays;
042:
043: import org.apache.commons.logging.Log;
044: import org.apache.commons.logging.LogFactory;
045: import org.apache.lucene.document.Document;
046: import org.apache.lucene.index.IndexWriter;
047:
048: /**
049: * <a href="FileSystemHook.java.html"><b><i>View Source</i></b></a>
050: *
051: * @author Brian Wing Shun Chan
052: * @author Sten Martinez
053: *
054: */
055: public class FileSystemHook extends BaseHook {
056:
057: public FileSystemHook() {
058: _rootDir = new File(_ROOT_DIR);
059:
060: if (!_rootDir.exists()) {
061: _rootDir.mkdirs();
062: }
063: }
064:
065: public void addDirectory(long companyId, long repositoryId,
066: String dirName) throws PortalException, SystemException {
067:
068: File dirNameDir = getDirNameDir(companyId, repositoryId,
069: dirName);
070:
071: if (dirNameDir.exists()) {
072: throw new DuplicateDirectoryException();
073: }
074:
075: dirNameDir.mkdirs();
076: }
077:
078: public void addFile(long companyId, String portletId, long groupId,
079: long repositoryId, String fileName, String properties,
080: String[] tagsEntries, InputStream is)
081: throws PortalException, SystemException {
082:
083: try {
084: File fileNameVersionFile = getFileNameVersionFile(
085: companyId, repositoryId, fileName, DEFAULT_VERSION);
086:
087: if (fileNameVersionFile.exists()) {
088: throw new DuplicateFileException();
089: }
090:
091: FileUtil.write(fileNameVersionFile, is);
092:
093: Indexer.addFile(companyId, portletId, groupId,
094: repositoryId, fileName, properties, tagsEntries);
095: } catch (IOException ioe) {
096: throw new SystemException();
097: }
098: }
099:
100: public void checkRoot(long companyId) throws SystemException {
101: }
102:
103: public void deleteDirectory(long companyId, String portletId,
104: long repositoryId, String dirName) throws PortalException,
105: SystemException {
106:
107: File dirNameDir = getDirNameDir(companyId, repositoryId,
108: dirName);
109:
110: if (!dirNameDir.exists()) {
111: throw new NoSuchDirectoryException();
112: }
113:
114: FileUtil.deltree(dirNameDir);
115: }
116:
117: public void deleteFile(long companyId, String portletId,
118: long repositoryId, String fileName) throws PortalException,
119: SystemException {
120:
121: try {
122: File fileNameDir = getFileNameDir(companyId, repositoryId,
123: fileName);
124:
125: if (!fileNameDir.exists()) {
126: throw new NoSuchFileException();
127: }
128:
129: FileUtil.deltree(fileNameDir);
130:
131: Indexer.deleteFile(companyId, portletId, repositoryId,
132: fileName);
133: } catch (IOException ioe) {
134: throw new SystemException();
135: }
136: }
137:
138: public void deleteFile(long companyId, String portletId,
139: long repositoryId, String fileName, double versionNumber)
140: throws PortalException, SystemException {
141:
142: File fileNameVersionFile = getFileNameVersionFile(companyId,
143: repositoryId, fileName, versionNumber);
144:
145: if (!fileNameVersionFile.exists()) {
146: throw new NoSuchFileException();
147: }
148:
149: fileNameVersionFile.delete();
150: }
151:
152: public InputStream getFileAsStream(long companyId,
153: long repositoryId, String fileName, double versionNumber)
154: throws PortalException, SystemException {
155:
156: try {
157: if (versionNumber == 0) {
158: versionNumber = getHeadVersionNumber(companyId,
159: repositoryId, fileName);
160: }
161:
162: File fileNameVersionFile = getFileNameVersionFile(
163: companyId, repositoryId, fileName, versionNumber);
164:
165: if (!fileNameVersionFile.exists()) {
166: throw new NoSuchFileException();
167: }
168:
169: return new FileInputStream(fileNameVersionFile);
170: } catch (IOException ioe) {
171: throw new SystemException();
172: }
173: }
174:
175: public String[] getFileNames(long companyId, long repositoryId,
176: String dirName) throws PortalException, SystemException {
177:
178: try {
179: File dirNameDir = getDirNameDir(companyId, repositoryId,
180: dirName);
181:
182: if (!dirNameDir.exists()) {
183: throw new NoSuchDirectoryException();
184: }
185:
186: String[] fileNames = FileUtil.listDirs(dirNameDir);
187:
188: Arrays.sort(fileNames);
189:
190: // Convert /${fileName} to /${dirName}/${fileName}
191:
192: for (int i = 0; i < fileNames.length; i++) {
193: fileNames[i] = StringPool.SLASH + dirName
194: + StringPool.SLASH + fileNames[i];
195: }
196:
197: return fileNames;
198: } catch (IOException ioe) {
199: throw new SystemException();
200: }
201: }
202:
203: public long getFileSize(long companyId, long repositoryId,
204: String fileName) throws PortalException, SystemException {
205:
206: try {
207: double versionNumber = getHeadVersionNumber(companyId,
208: repositoryId, fileName);
209:
210: File fileNameVersionFile = getFileNameVersionFile(
211: companyId, repositoryId, fileName, versionNumber);
212:
213: if (!fileNameVersionFile.exists()) {
214: throw new NoSuchFileException();
215: }
216:
217: return fileNameVersionFile.length();
218: } catch (IOException ioe) {
219: throw new SystemException();
220: }
221: }
222:
223: public boolean hasFile(long companyId, long repositoryId,
224: String fileName, double versionNumber)
225: throws PortalException, SystemException {
226:
227: File fileNameVersionFile = getFileNameVersionFile(companyId,
228: repositoryId, fileName, versionNumber);
229:
230: if (fileNameVersionFile.exists()) {
231: return true;
232: } else {
233: return false;
234: }
235: }
236:
237: public void move(String srcDir, String destDir)
238: throws SystemException {
239: }
240:
241: public void reIndex(String[] ids) throws SearchException {
242: long companyId = GetterUtil.getLong(ids[0]);
243: String portletId = ids[1];
244: long groupId = GetterUtil.getLong(ids[2]);
245: long repositoryId = GetterUtil.getLong(ids[3]);
246:
247: IndexWriter writer = null;
248:
249: try {
250: writer = LuceneUtil.getWriter(companyId);
251:
252: File repistoryDir = getRepositoryDir(companyId,
253: repositoryId);
254:
255: String[] fileNames = FileUtil.listDirs(repistoryDir);
256:
257: for (int i = 0; i < fileNames.length; i++) {
258: String fileName = fileNames[i];
259:
260: try {
261: Document doc = Indexer.getAddFileDocument(
262: companyId, portletId, groupId,
263: repositoryId, fileName);
264:
265: writer.addDocument(doc);
266: } catch (Exception e) {
267: _log.error("Reindexing " + fileName, e);
268: }
269: }
270: } catch (IOException ioe) {
271: throw new SearchException(ioe);
272: } finally {
273: try {
274: if (writer != null) {
275: LuceneUtil.write(companyId);
276: }
277: } catch (Exception e) {
278: _log.error(e);
279: }
280: }
281: }
282:
283: public void updateFile(long companyId, String portletId,
284: long groupId, long repositoryId, String fileName,
285: double versionNumber, String sourceFileName,
286: String properties, String[] tagsEntries, InputStream is)
287: throws PortalException, SystemException {
288:
289: try {
290: File fileNameVersionFile = getFileNameVersionFile(
291: companyId, repositoryId, fileName, versionNumber);
292:
293: if (fileNameVersionFile.exists()) {
294: throw new DuplicateFileException();
295: }
296:
297: FileUtil.write(fileNameVersionFile, is);
298:
299: Indexer.updateFile(companyId, portletId, groupId,
300: repositoryId, fileName, properties, tagsEntries);
301: } catch (IOException ioe) {
302: throw new SystemException();
303: }
304: }
305:
306: public void updateFile(long companyId, String portletId,
307: long groupId, long repositoryId, long newRepositoryId,
308: String fileName) throws PortalException, SystemException {
309:
310: try {
311: File fileNameDir = getFileNameDir(companyId, repositoryId,
312: fileName);
313: File newFileNameDir = getFileNameDir(companyId,
314: newRepositoryId, fileName);
315:
316: FileUtil.copyDirectory(fileNameDir, newFileNameDir);
317:
318: FileUtil.deltree(fileNameDir);
319:
320: try {
321: Indexer.deleteFile(companyId, portletId, repositoryId,
322: fileName);
323: } catch (IOException ioe) {
324: }
325:
326: Indexer.addFile(companyId, portletId, groupId,
327: newRepositoryId, fileName);
328: } catch (IOException ioe) {
329: throw new SystemException();
330: }
331: }
332:
333: protected File getCompanyDir(long companyId) {
334: File companyDir = new File(_rootDir + StringPool.SLASH
335: + companyId);
336:
337: if (!companyDir.exists()) {
338: companyDir.mkdirs();
339: }
340:
341: return companyDir;
342: }
343:
344: protected File getDirNameDir(long companyId, long repositoryId,
345: String dirName) {
346:
347: return getFileNameDir(companyId, repositoryId, dirName);
348: }
349:
350: protected File getRepositoryDir(long companyId, long repositoryId) {
351: File companyDir = getCompanyDir(companyId);
352:
353: File repositoryDir = new File(companyDir + StringPool.SLASH
354: + repositoryId);
355:
356: if (!repositoryDir.exists()) {
357: repositoryDir.mkdirs();
358: }
359:
360: return repositoryDir;
361: }
362:
363: protected File getFileNameDir(long companyId, long repositoryId,
364: String fileName) {
365:
366: File repositoryDir = getRepositoryDir(companyId, repositoryId);
367:
368: File fileNameDir = new File(repositoryDir + StringPool.SLASH
369: + fileName);
370:
371: return fileNameDir;
372: }
373:
374: protected File getFileNameVersionFile(long companyId,
375: long repositoryId, String fileName, double version) {
376:
377: File fileNameDir = getFileNameDir(companyId, repositoryId,
378: fileName);
379:
380: File fileNameVersionFile = new File(fileNameDir
381: + StringPool.SLASH + version);
382:
383: return fileNameVersionFile;
384: }
385:
386: protected double getHeadVersionNumber(long companyId,
387: long repositoryId, String fileName) throws IOException {
388:
389: File fileNameDir = getFileNameDir(companyId, repositoryId,
390: fileName);
391:
392: if (!fileNameDir.exists()) {
393: return DEFAULT_VERSION;
394: }
395:
396: String[] versionNumbers = FileUtil.listFiles(fileNameDir);
397:
398: double headVersionNumber = DEFAULT_VERSION;
399:
400: for (int i = 0; i < versionNumbers.length; i++) {
401: double versionNumber = GetterUtil
402: .getDouble(versionNumbers[i]);
403:
404: if (versionNumber > headVersionNumber) {
405: headVersionNumber = versionNumber;
406: }
407: }
408:
409: return headVersionNumber;
410: }
411:
412: private static final String _ROOT_DIR = PropsUtil
413: .get(PropsUtil.DL_HOOK_FILE_SYSTEM_ROOT_DIR);
414:
415: private static Log _log = LogFactory.getLog(FileSystemHook.class);
416:
417: private File _rootDir;
418:
419: }
|