Source Code Cross Referenced for SolutionRepositoryBase.java in  » Report » pentaho-report » org » pentaho » core » repository » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Report » pentaho report » org.pentaho.core.repository 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.pentaho.core.repository;
002:
003:        import java.io.ByteArrayInputStream;
004:        import java.io.ByteArrayOutputStream;
005:        import java.io.File;
006:        import java.io.FileInputStream;
007:        import java.io.FileNotFoundException;
008:        import java.io.FileOutputStream;
009:        import java.io.IOException;
010:        import java.io.InputStream;
011:        import java.io.InputStreamReader;
012:        import java.io.Reader;
013:        import java.math.BigDecimal;
014:        import java.nio.channels.FileChannel;
015:        import java.util.HashMap;
016:        import java.util.Iterator;
017:        import java.util.List;
018:        import java.util.Locale;
019:        import java.util.Properties;
020:        import java.util.regex.Matcher;
021:        import java.util.regex.Pattern;
022:
023:        import javax.activation.DataSource;
024:
025:        import org.apache.commons.logging.Log;
026:        import org.apache.commons.logging.LogFactory;
027:        import org.dom4j.Document;
028:        import org.dom4j.DocumentHelper;
029:        import org.dom4j.Element;
030:        import org.dom4j.Node;
031:        import org.pentaho.core.audit.AuditHelper;
032:        import org.pentaho.core.audit.MessageTypes;
033:        import org.pentaho.core.cache.CacheManager;
034:        import org.pentaho.core.session.IPentahoSession;
035:        import org.pentaho.core.solution.ActionResource;
036:        import org.pentaho.core.solution.IActionResource;
037:        import org.pentaho.core.solution.ISolutionFile;
038:        import org.pentaho.core.solution.SolutionReposUtil;
039:        import org.pentaho.core.solution.SolutionReposUtil.ISolutionFilter;
040:        import org.pentaho.core.system.PentahoMessenger;
041:        import org.pentaho.core.system.PentahoSystem;
042:        import org.pentaho.core.util.CleanXmlHelper;
043:        import org.pentaho.core.util.XmlHelper;
044:        import org.pentaho.messages.Messages;
045:        import org.pentaho.messages.util.LocaleHelper;
046:        import org.pentaho.repository.filebased.solution.FileSolutionFile;
047:        import org.pentaho.util.HttpUtil;
048:
049:        import com.pentaho.repository.dbbased.solution.RepositoryFile;
050:
051:        public abstract class SolutionRepositoryBase extends PentahoMessenger
052:                implements  ISolutionRepository {
053:            private static final long serialVersionUID = 6367444546398801343L;
054:
055:            protected static final Log logger = LogFactory
056:                    .getLog(SolutionRepositoryBase.class);
057:
058:            protected static final int BROWSE_DEPTH = 2;
059:
060:            protected static final String ROOT_NODE_NAME = "repositiory"; //$NON-NLS-1$
061:
062:            protected static final String LOCATION_ATTR_NAME = "location"; //$NON-NLS-1$
063:
064:            protected static final String EMPTY_STR = ""; //$NON-NLS-1$
065:
066:            protected static final boolean debug = PentahoSystem.debug;
067:
068:            protected static final String ENTRY_NODE_NAME = "entry"; //$NON-NLS-1$
069:
070:            protected static final String TYPE_ATTR_NAME = "type"; //$NON-NLS-1$
071:
072:            protected static final String NAME_ATTR_NAME = "name"; //$NON-NLS-1$
073:
074:            protected static final String DIRECTORY_ATTR = "directory"; //$NON-NLS-1$
075:
076:            protected static final String FILE_ATTR = "file"; //$NON-NLS-1$
077:
078:            public static final String INDEX_FILENAME = "index.xml"; //$NON-NLS-1$
079:
080:            protected static final long PUBLISH_TIMEOUT = 1500; // 1.5 seconds
081:
082:            protected static HashMap propertyMap = new HashMap();
083:
084:            protected static String LOG_NAME = "SOLUTION-REPOSITORY"; //$NON-NLS-1$
085:            /*
086:             * matches 0 or 1 "/" followed by any non-"/" followed by
087:             * either an end of string or (a "/" followed by 0 or more of anything).
088:             */
089:            private static final String RE_SYSTEM_PATH = "^[/\\\\]?system($|[/\\\\].*$)"; //$NON-NLS-1$
090:            private static final Pattern SYSTEM_PATH_PATTERN = Pattern
091:                    .compile(SolutionRepositoryBase.RE_SYSTEM_PATH);
092:
093:            protected ThreadLocal session = new ThreadLocal();
094:
095:            protected String rootPath;
096:
097:            protected File rootFile;
098:
099:            private CacheManager cacheManager;
100:
101:            protected boolean cachingAvailable = false;
102:
103:            public Log getLogger() {
104:                return logger;
105:            }
106:
107:            protected Locale getLocale() {
108:                return LocaleHelper.getLocale();
109:            }
110:
111:            public void init() {
112:                cacheManager = PentahoSystem.getCacheManager();
113:                cachingAvailable = cacheManager != null
114:                        && cacheManager.cacheEnabled();
115:                rootFile = getFile("", false); //$NON-NLS-1$
116:                rootPath = rootFile.getAbsolutePath() + File.separator;
117:                setLogId(LOG_NAME + ": "); //$NON-NLS-1$
118:            }
119:
120:            public void init(IPentahoSession pentahoSession) {
121:                init();
122:                setSession(pentahoSession);
123:            }
124:
125:            public IPentahoSession getSession() {
126:                Object threadSession = session.get();
127:                return (IPentahoSession) threadSession;
128:            }
129:
130:            public void setSession(IPentahoSession inSession) {
131:                session.set(inSession);
132:            }
133:
134:            // TODO sbarkdull, this code is very similar to XmlHelper.getLocalizedFile(). they
135:            // likely should be resolved into a single method in an appropriate utility class/package.
136:            public ISolutionFile getLocalizedFile(ISolutionFile resourceFile) {
137:                String fileName = resourceFile.getFileName();
138:                int idx = fileName.lastIndexOf('.');
139:                String baseName = idx == -1 ? fileName : fileName.substring(0,
140:                        idx); // These two lines fix an index out of bounds
141:                String extension = idx == -1 ? "" : fileName.substring(idx); // Exception that occurs when a filename has no extension //$NON-NLS-1$
142:                String directory = resourceFile.getSolutionPath();
143:                if (directory.lastIndexOf(fileName) != -1) {
144:                    directory = new StringBuffer(directory)
145:                            .delete(directory.lastIndexOf(fileName),
146:                                    directory.length()).toString();
147:                }
148:                if (!directory.endsWith(RepositoryFile.SEPARATOR + "")) { //$NON-NLS-1$
149:                    directory += RepositoryFile.SEPARATOR;
150:                }
151:                String language = getLocale().getLanguage();
152:                String country = getLocale().getCountry();
153:                String variant = getLocale().getVariant();
154:                ISolutionFile localeFile = null;
155:                if (!variant.equals("")) { //$NON-NLS-1$
156:                    localeFile = getFileByPath(directory
157:                            + baseName
158:                            + "_" + language + "_" + country + "_" + variant + extension); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
159:                }
160:                if (localeFile == null) {
161:                    localeFile = getFileByPath(directory + baseName
162:                            + "_" + language + "_" + country + extension); //$NON-NLS-1$//$NON-NLS-2$
163:                }
164:                if (localeFile == null) {
165:                    localeFile = getFileByPath(directory + baseName
166:                            + "_" + language + extension); //$NON-NLS-1$
167:                }
168:                if (localeFile == null) {
169:                    localeFile = getFileByPath(directory + baseName + extension);
170:                }
171:                if (localeFile != null) {
172:                    return localeFile;
173:                } else {
174:                    return resourceFile;
175:                }
176:            }
177:
178:            public ISolutionFile getFileByPath(String path) {
179:                File file = new File(PentahoSystem.getApplicationContext()
180:                        .getSolutionPath(path));
181:                if (file.exists()) {
182:                    return new FileSolutionFile(file, rootFile);
183:                } else {
184:                    return null;
185:                }
186:            }
187:
188:            protected File getFile(String path, boolean create) {
189:                File f = new File(PentahoSystem.getApplicationContext()
190:                        .getSolutionPath(path));
191:                if (!f.exists() && !create) {
192:                    error(Messages
193:                            .getErrorString(
194:                                    "SolutionRepository.ERROR_0001_FILE_DOES_NOT_EXIST", path)); //$NON-NLS-1$
195:                    return null;
196:                }
197:                if (!f.exists()) {
198:                    f.mkdirs();
199:                }
200:                // TODO: caching
201:                if (debug)
202:                    debug(Messages
203:                            .getErrorString(
204:                                    "SolutionRepository.DEBUG_FILE_PATH", f.getAbsolutePath())); //$NON-NLS-1$
205:                return f;
206:            }
207:
208:            public static boolean isSystemPath(String path) {
209:
210:                Matcher m = SYSTEM_PATH_PATTERN.matcher(path.toLowerCase());
211:                return m.matches();
212:            }
213:
214:            public InputStream getResourceInputStream(
215:                    IActionResource actionResource, boolean getLocalizedResource)
216:                    throws FileNotFoundException {
217:                int resourceSource = actionResource.getSourceType();
218:                InputStream inputStream = null;
219:                if (resourceSource == IActionResource.URL_RESOURCE) {
220:                    inputStream = HttpUtil.getURLInputStream(actionResource
221:                            .getAddress());
222:                } else if ((resourceSource == IActionResource.SOLUTION_FILE_RESOURCE)
223:                        || (resourceSource == IActionResource.FILE_RESOURCE)) {
224:                    ISolutionFile solutionFile = getSolutionFile(actionResource);
225:                    if (solutionFile == null) {
226:                        String msg = Messages
227:                                .getErrorString(
228:                                        "SOLREPO.ERROR_0006_MISSING_RESOURCE", actionResource.getAddress()); //$NON-NLS-1$
229:                        error(msg);
230:                        throw new FileNotFoundException(msg);
231:                    } else if (getLocalizedResource) {
232:                        solutionFile = getLocalizedFile(solutionFile);
233:                    }
234:                    inputStream = new ByteArrayInputStream(solutionFile
235:                            .getData());
236:                }
237:                return inputStream;
238:            }
239:
240:            public void localizeDoc(Node document, ISolutionFile file) {
241:                String fileName = file.getFileName();
242:                int dotIndex = fileName.indexOf('.');
243:                String baseName = fileName.substring(0, dotIndex);
244:                // TODO read in nodes from the locale file and use them to override the
245:                // ones in the main document
246:                try {
247:                    List nodes = document.selectNodes("descendant::*"); //$NON-NLS-1$
248:                    Iterator nodeIterator = nodes.iterator();
249:                    while (nodeIterator.hasNext()) {
250:                        Node node = (Node) nodeIterator.next();
251:                        String name = node.getText();
252:                        if (name.startsWith("%") && !node.getPath().endsWith("/text()")) { //$NON-NLS-1$ //$NON-NLS-2$
253:                            try {
254:                                String localeText = getLocaleString(name,
255:                                        baseName, file);
256:                                if (localeText != null) {
257:                                    node.setText(localeText);
258:                                }
259:                            } catch (Exception e) {
260:                                warn(Messages
261:                                        .getString(
262:                                                "SolutionRepository.WARN_MISSING_RESOURCE_PROPERTY", name.substring(1), baseName, getLocale().toString())); //$NON-NLS-1$
263:                            }
264:                        }
265:                    }
266:                } catch (Exception e) {
267:                    error(
268:                            Messages
269:                                    .getErrorString(
270:                                            "SolutionRepository.ERROR_0007_COULD_NOT_READ_PROPERTIES", file.getFullPath()), e); //$NON-NLS-1$
271:                }
272:            }
273:
274:            public String getLocaleString(String key, String baseName,
275:                    String baseFilePath) {
276:                ISolutionFile file = getFileByPath(baseFilePath);
277:                return getLocaleString(key, baseName, file);
278:            }
279:
280:            // TODO sbarkdull, needs to be refactored, consider if
281:            // how it should work with/, etc. XmlHelper getLocalizedFile
282:            protected String getLocaleString(String key, String baseName,
283:                    ISolutionFile baseFile) {
284:                ISolutionFile searchDir = baseFile.retrieveParent();
285:                try {
286:                    boolean searching = true;
287:                    while (searching) {
288:                        String localeText = getLocaleText(key,
289:                                getFileByPath(searchDir.getFullPath()
290:                                        + RepositoryFile.SEPARATOR + baseName
291:                                        + '_' + getLocale().getLanguage() + '_'
292:                                        + getLocale().getCountry() + '_'
293:                                        + getLocale().getVariant()
294:                                        + ".properties")); //$NON-NLS-1$
295:                        if (localeText == null) {
296:                            localeText = getLocaleText(key,
297:                                    getFileByPath(searchDir.getFullPath()
298:                                            + RepositoryFile.SEPARATOR
299:                                            + baseName + '_'
300:                                            + getLocale().getLanguage() + '_'
301:                                            + getLocale().getCountry()
302:                                            + ".properties")); //$NON-NLS-1$)
303:                            if (localeText == null) {
304:                                localeText = getLocaleText(key,
305:                                        getFileByPath(searchDir.getFullPath()
306:                                                + RepositoryFile.SEPARATOR
307:                                                + baseName + '_'
308:                                                + getLocale().getLanguage()
309:                                                + ".properties")); //$NON-NLS-1$
310:                                if (localeText == null) {
311:                                    localeText = getLocaleText(key,
312:                                            getFileByPath(searchDir
313:                                                    .getFullPath()
314:                                                    + RepositoryFile.SEPARATOR
315:                                                    + baseName + ".properties")); //$NON-NLS-1$);
316:                                }
317:                            }
318:                        }
319:                        if (localeText != null) {
320:                            return localeText;
321:                        }
322:                        if (searching) {
323:                            if (!baseName.equals("messages")) { //$NON-NLS-1$
324:                                baseName = "messages"; //$NON-NLS-1$
325:                            } else {
326:                                if (searchDir.isRoot()) {
327:                                    searching = false;
328:                                } else {
329:                                    searchDir = searchDir.retrieveParent();
330:                                }
331:                            }
332:                        }
333:                    }
334:                    return null;
335:                } catch (Exception e) {
336:                    error(
337:                            Messages
338:                                    .getErrorString(
339:                                            "SolutionRepository.ERROR_0007_COULD_NOT_READ_PROPERTIES", baseFile.getFullPath()), e); //$NON-NLS-1$
340:                }
341:                return null;
342:            }
343:
344:            protected String getLocaleText(String key, ISolutionFile file)
345:                    throws IOException {
346:                if (file != null) {
347:                    Properties p = (Properties) propertyMap.get(file
348:                            .getFullPath());
349:                    if (p == null) {
350:                        p = new Properties();
351:                        p.load(new ByteArrayInputStream(file.getData()));
352:                        propertyMap.put(file.getFullPath(), p);
353:                    }
354:                    String localeText = p.getProperty(key.substring(1));
355:                    if (localeText != null) {
356:                        return localeText;
357:                    }
358:                }
359:                return null;
360:            }
361:
362:            public InputStream getResourceInputStream(String solutionPath,
363:                    boolean getLocalizedResource) throws FileNotFoundException {
364:                ActionResource resource = new ActionResource(
365:                        "", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$ //$NON-NLS-2$
366:                        solutionPath);
367:                return getResourceInputStream(resource, getLocalizedResource);
368:            }
369:
370:            public Reader getResourceReader(IActionResource actionResource)
371:                    throws FileNotFoundException, IOException {
372:                return new InputStreamReader(getResourceInputStream(
373:                        actionResource, true), LocaleHelper.getSystemEncoding());
374:            }
375:
376:            public Reader getResourceReader(String solutionPath)
377:                    throws FileNotFoundException, IOException {
378:                ActionResource resource = new ActionResource(
379:                        "", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$ //$NON-NLS-2$
380:                        solutionPath);
381:                return getResourceReader(resource);
382:            }
383:
384:            public String getResourceAsString(IActionResource actionResource)
385:                    throws IOException {
386:                return new String(getResourceAsBytes(actionResource, true),
387:                        LocaleHelper.getSystemEncoding());
388:            }
389:
390:            public String getResourceAsString(String solutionPath)
391:                    throws IOException {
392:                ActionResource resource = new ActionResource(
393:                        "", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$ //$NON-NLS-2$
394:                        solutionPath);
395:                return getResourceAsString(resource);
396:            }
397:
398:            public byte[] getResourceAsBytes(IActionResource actionResource,
399:                    boolean getLocalizedResource) throws IOException {
400:                InputStream inputStream = getResourceInputStream(
401:                        actionResource, getLocalizedResource);
402:                ByteArrayOutputStream bais = new ByteArrayOutputStream();
403:                int numRead = 0;
404:                byte[] buffer = new byte[16384];
405:                while ((numRead = inputStream.read(buffer)) != -1) {
406:                    bais.write(buffer, 0, numRead);
407:                }
408:                bais.close();
409:                return bais.toByteArray();
410:            }
411:
412:            public byte[] getResourceAsBytes(String solutionPath,
413:                    boolean getLocalizedResource) throws IOException {
414:                ActionResource resource = new ActionResource(
415:                        "", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$ //$NON-NLS-2$
416:                        solutionPath);
417:                return getResourceAsBytes(resource, getLocalizedResource);
418:            }
419:
420:            public DataSource getResourceDataSource(
421:                    IActionResource actionResource)
422:                    throws FileNotFoundException {
423:                return new InputStreamDataSource(actionResource.getName(),
424:                        getResourceInputStream(actionResource, true));
425:            }
426:
427:            public DataSource getResourceDataSource(String solutionPath)
428:                    throws FileNotFoundException {
429:                ActionResource resource = new ActionResource(
430:                        "", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$ //$NON-NLS-2$
431:                        solutionPath);
432:                return getResourceDataSource(resource);
433:            }
434:
435:            public Document getResourceAsDocument(String solutionPath)
436:                    throws IOException {
437:                ActionResource resource = new ActionResource(
438:                        "", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$ //$NON-NLS-2$
439:                        solutionPath);
440:                return getResourceAsDocument(resource);
441:            }
442:
443:            public Document getResourceAsDocument(IActionResource actionResource)
444:                    throws IOException {
445:                // TODO support locales here
446:
447:                // figure out what the XML string says the encoding is
448:                byte[] b = getResourceAsBytes(actionResource, true);
449:                String tmpXml = new String(b);
450:                String encoding = CleanXmlHelper.getEncoding(tmpXml);
451:                if (null == encoding) {
452:                    encoding = LocaleHelper.getSystemEncoding();
453:                }
454:                // now apply the correct encoding when we translate the bytes to a String
455:                String xml = new String(b, encoding);
456:                Document document = null;
457:                try {
458:                    document = XmlHelper.getDocFromString(xml);
459:                } catch (Throwable t) {
460:                    error(
461:                            Messages
462:                                    .getErrorString(
463:                                            "SolutionRepository.ERROR_0009_INVALID_DOCUMENT", actionResource.getAddress()), t); //$NON-NLS-1$
464:                    return null;
465:                }
466:                return document;
467:            }
468:
469:            public Document getSolutions(int actionOperation) {
470:                return getSolutions(null, null, actionOperation, false);
471:            }
472:
473:            protected List getSolutionNames(String solutionName,
474:                    String pathName, int actionOperation, boolean visibleOnly) {
475:                Document solns = getSolutions(solutionName, pathName,
476:                        actionOperation, visibleOnly);
477:                String xPath = "/repository/file"; //$NON-NLS-1$
478:                return solns.selectNodes(xPath);
479:            }
480:
481:            /**
482:             * Clears cached data for ALL users
483:             */
484:            public void resetRepository() {
485:                if (cacheManager != null) {
486:                    cacheManager.killSessionCaches();
487:                }
488:            }
489:
490:            public ISolutionFile getRootFolder() {
491:                return new FileSolutionFile(rootFile, rootFile);
492:            }
493:
494:            public String getXSLName(Document document, String solution,
495:                    String inputXSLName) {
496:                String xslName = inputXSLName;
497:                if (solution == null) {
498:                    if (xslName == null) {
499:                        Node node = document
500:                                .selectSingleNode("/repository/@displaytype"); //$NON-NLS-1$
501:                        if (node != null) {
502:                            String displayType = node.getText();
503:                            if (displayType.endsWith(".xsl")) { //$NON-NLS-1$
504:                                // this folder has a custom XSL
505:                                xslName = displayType;
506:                            }
507:                        }
508:                    }
509:                } else {
510:                    if (xslName == null) {
511:                        Node node = document
512:                                .selectSingleNode("/files/@displaytype"); //$NON-NLS-1$
513:                        if (node != null) {
514:                            String displayType = node.getText();
515:                            if (displayType.endsWith(".xsl")) { //$NON-NLS-1$
516:                                // this folder has a custom XSL
517:                                xslName = displayType;
518:                            }
519:                        }
520:                    }
521:                }
522:                return xslName;
523:            }
524:
525:            public Document getNavigationUIDocument(String solution,
526:                    String path, int actionOperation) {
527:                Document document = this .getSolutions(solution, path,
528:                        ISolutionRepository.ACTION_EXECUTE, false);
529:                return document;
530:            }
531:
532:            /**
533:             * @return int possible values:
534:             * 	ISolutionRepository.FILE_ADD_SUCCESSFUL
535:             * 	ISolutionRepository.FILE_EXISTS
536:             * 	ISolutionRepository.FILE_ADD_FAILED
537:             */
538:            public int addSolutionFile(String baseUrl, String path,
539:                    String fileName, File f, boolean overwrite) {
540:                if (!path.endsWith("/") && !path.endsWith("\\")) { //$NON-NLS-1$ //$NON-NLS-2$
541:                    path += File.separator;
542:                }
543:                File fNew = new File(baseUrl + path + fileName);
544:                int status = ISolutionRepository.FILE_ADD_SUCCESSFUL;
545:                if (fNew.exists() && !overwrite) {
546:                    status = ISolutionRepository.FILE_EXISTS;
547:                } else {
548:                    FileChannel in = null, out = null;
549:                    try {
550:                        in = new FileInputStream(f).getChannel();
551:                        out = new FileOutputStream(fNew).getChannel();
552:                        out.transferFrom(in, 0, in.size());
553:                        resetRepository();
554:                    } catch (Exception e) {
555:                        logger.error(e.toString());
556:                        status = ISolutionRepository.FILE_ADD_FAILED;
557:                    } finally {
558:                        try {
559:                            if (in != null)
560:                                in.close();
561:                            if (out != null)
562:                                out.close();
563:                        } catch (Exception e) {
564:                            // TODO, we should probably log the error, and return a failure status
565:                        }
566:                    }
567:                }
568:                return status;
569:            }
570:
571:            public int addSolutionFile(String baseUrl, String path,
572:                    String fileName, byte[] data, boolean overwrite) {
573:                // baseUrl = baseUrl + path;
574:                if (!path.endsWith("/") && !path.endsWith("\\")) { //$NON-NLS-1$//$NON-NLS-2$
575:                    path += File.separator;
576:                }
577:                File fNew = new File(baseUrl + path + fileName);
578:                int status = ISolutionRepository.FILE_ADD_SUCCESSFUL;
579:                if (fNew.exists() && !overwrite) {
580:                    status = ISolutionRepository.FILE_EXISTS;
581:                } else {
582:                    FileOutputStream fNewOut = null;
583:                    try {
584:                        fNewOut = new FileOutputStream(fNew);
585:                        fNewOut.write(data);
586:                        resetRepository();
587:                    } catch (Exception e) {
588:                        status = ISolutionRepository.FILE_ADD_FAILED;
589:                        logger.error(e.toString());
590:                    } finally {
591:                        try {
592:                            fNewOut.close();
593:                        } catch (Exception e) {
594:                            // TODO, we should probably log the error, and return a failure status
595:                        }
596:                    }
597:                }
598:                return status;
599:            }
600:
601:            public boolean removeSolutionFile(String solutionPath) {
602:                solutionPath = PentahoSystem.getApplicationContext()
603:                        .getSolutionPath(solutionPath);
604:                File deleteFile = new File(solutionPath);
605:                try {
606:                    if (deleteFile.exists()) {
607:                        if (!deleteFile.isDirectory()) {
608:                            boolean deleted = deleteFile.delete();
609:                            if (deleted) {
610:                                AuditHelper
611:                                        .audit(
612:                                                "", getSession().getName(), "", getClass().toString(), "", MessageTypes.UNKNOWN_ENTRY, Messages.getString("SOLREPO.AUDIT_DEL_FILE", solutionPath), "", new BigDecimal("0.0"), null); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
613:                            }
614:                            return deleted;
615:                        } else { // recursively delete all the files under this directory
616:                            // and then delete the directory
617:                            return deleteFolder(deleteFile);
618:                        }
619:                    }
620:                } finally {
621:                    resetRepository();
622:                }
623:                return false;
624:            }
625:
626:            /**
627:             * this is the file based removeSolutionFile, used by subclasses
628:             */
629:            public boolean removeSolutionFile(String solution, String path,
630:                    String fileName) {
631:                return removeSolutionFile(solution + path + fileName);
632:            }
633:
634:            private boolean deleteFolder(File dir) {
635:                if (!dir.isDirectory()) {
636:                    logger
637:                            .warn(Messages
638:                                    .getString("SolutionRepository.USER_DELETE_FOLDER_WARNING")); //$NON-NLS-1$
639:                    return false;
640:                }
641:                String[] files = dir.list();
642:                for (int i = 0; i < files.length; i++) {
643:                    String filePath = dir.getAbsolutePath() + File.separator
644:                            + files[i];
645:                    File file = new File(filePath);
646:                    if (file.isDirectory()) {
647:                        if (deleteFolder(file)) {
648:                            AuditHelper
649:                                    .audit(
650:                                            "", getSession().getName(), "", getClass().toString(), "", MessageTypes.UNKNOWN_ENTRY, Messages.getString("SOLREPO.AUDIT_DEL_FOLDER", filePath), "", new BigDecimal("0.0"), null); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
651:                        }
652:                    } else {
653:                        if (file.delete()) {
654:                            AuditHelper
655:                                    .audit(
656:                                            "", getSession().getName(), "", getClass().toString(), "", MessageTypes.UNKNOWN_ENTRY, Messages.getString("SOLREPO.AUDIT_DEL_FILE", filePath), "", new BigDecimal("0.0"), null); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
657:                        }
658:                    }
659:                }
660:                String filePath = dir.getAbsolutePath();
661:                boolean deleted = dir.delete();
662:                if (deleted) {
663:                    AuditHelper
664:                            .audit(
665:                                    "", getSession().getName(), "", getClass().toString(), "", MessageTypes.UNKNOWN_ENTRY, Messages.getString("SOLREPO.AUDIT_DEL_FOLDER", filePath), "", new BigDecimal("0.0"), null); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
666:                }
667:                return deleted;
668:            }
669:
670:            public Document getFullSolutionTree(int actionOperation,
671:                    ISolutionFilter filter) {
672:
673:                Document document = DocumentHelper.createDocument();
674:                Element root = document
675:                        .addElement(SolutionReposUtil.TREE_NODE_NAME);
676:
677:                SolutionReposUtil.processSolutionTree(root,
678:                        new FileSolutionFile(rootFile, rootFile),
679:                        SolutionReposUtil.KEEP_ALL_FILTER,
680:                        SolutionReposUtil.ADD_NOTHING_CONTRIBUTOR, null, null,
681:                        actionOperation);
682:
683:                return document;
684:            }
685:
686:            protected boolean isCachingAvailable() {
687:                return cachingAvailable;
688:            }
689:
690:            /**
691:             * Caches the repository object
692:             * 
693:             * @param key - String value of the key
694:             * @param value - Object referred to by key
695:             * 
696:             * @return null if unable to catch otherwise returns the object that was cached
697:             */
698:            protected Object putRepositoryObjectInCache(String key, Object value) {
699:                if (isCachingAvailable()) {
700:                    cacheManager.putInSessionCache(getSession(), key, value);
701:                    return value;
702:                }
703:
704:                return null;
705:            }
706:
707:            /**
708:             * Gets the object from the session cache defined by the parameter key
709:             * 
710:             * @param key - String value of the key to lookup
711:             * 
712:             * @return Object that is referred to by the key.  Null if not in the cache or if caching is unavailable
713:             */
714:            protected Object getRepositoryObjectFromCache(String key) {
715:                if (isCachingAvailable()) {
716:                    return cacheManager.getFromSessionCache(getSession(), key);
717:                }
718:
719:                return null;
720:            }
721:
722:            public ISolutionFile getSolutionFile(IActionResource actionResource) {
723:                ISolutionFile solutionFile = null;
724:                int resourceSource = actionResource.getSourceType();
725:                String realPath = null;
726:                if (resourceSource == IActionResource.SOLUTION_FILE_RESOURCE) {
727:                    realPath = actionResource.getAddress();
728:                    if (!SolutionRepositoryBase.isSystemPath(realPath)) {
729:                        solutionFile = getFileByPath(realPath);
730:                    } else {
731:                        realPath = PentahoSystem.getApplicationContext()
732:                                .getSolutionPath(actionResource.getAddress());
733:                        solutionFile = new FileSolutionFile(new File(realPath),
734:                                rootFile);
735:                    }
736:                } else if (resourceSource == IActionResource.FILE_RESOURCE) {
737:                    realPath = actionResource.getAddress();
738:                    solutionFile = new FileSolutionFile(new File(realPath),
739:                            rootFile);
740:                }
741:                if (solutionFile == null || !solutionFile.exists()) {
742:                    solutionFile = null;
743:                }
744:                return solutionFile;
745:            }
746:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.