[jboss-svn-commits] JBL Code SVN: r19396 - labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Apr 3 13:02:16 EDT 2008
Author: szimano
Date: 2008-04-03 13:02:16 -0400 (Thu, 03 Apr 2008)
New Revision: 19396
Modified:
labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileAttachmentDataSource.java
labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileMediaDataSource.java
Log:
lame unclosed streams
Modified: labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileAttachmentDataSource.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileAttachmentDataSource.java 2008-04-03 13:59:50 UTC (rev 19395)
+++ labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileAttachmentDataSource.java 2008-04-03 17:02:16 UTC (rev 19396)
@@ -35,24 +35,24 @@
import org.apache.log4j.Logger;
import org.jboss.wiki.configuration.ConfigurationParser;
-public class FileAttachmentDataSource implements AttachmentDataSource{
+public class FileAttachmentDataSource implements AttachmentDataSource {
private final static String OTHER_LANG_DIR = "LANG/";
-
+
private String pathToAttachments;
-
+
private String pathToAttTrash;
-
+
private final int BUF_SIZE = 32768;
-
+
private Logger log = Logger.getLogger(this.getClass());
-
+
private String langCode;
-
+
public FileAttachmentDataSource() {
init("");
}
-
+
private void init(String prefix) {
log = Logger.getLogger(FileMediaDataSource.class);
@@ -66,7 +66,8 @@
.getResourceAsStream(FileMediaDataSource.propFileName));
} catch (IOException ioe) {
- log.error("Can't load the file " + FileMediaDataSource.propFileName + "\n", ioe);
+ log.error("Can't load the file " + FileMediaDataSource.propFileName
+ + "\n", ioe);
}
loadProperties(fileDSProps, prefix);
@@ -78,7 +79,8 @@
// create trash directory for deleted attachments if it doesn't exists
- pathToAttTrash = pathToAttachments + "/" + AttachmentDataSource.attTrashName;
+ pathToAttTrash = pathToAttachments + "/"
+ + AttachmentDataSource.attTrashName;
File pathToAttachmentsFile = new File(pathToAttTrash);
@@ -87,15 +89,15 @@
}
}
-
+
private void loadProperties(Properties fileDSProps, String prefix) {
if (fileDSProps.getProperty("pathToMedia") != null) {
pathToAttachments = fileDSProps.getProperty("pathToAttachements")
+ (prefix.length() > 0 ? "/" + OTHER_LANG_DIR + prefix : "");
log.info("Path to attachements is: " + pathToAttachments);
-
+
File attDir = new File(pathToAttachments);
-
+
if (!attDir.exists()) {
attDir.mkdirs();
log.info("Dir for attachments created");
@@ -106,7 +108,7 @@
+ FileMediaDataSource.propFileName);
}
}
-
+
public WikiAttachment getAttachment(String pageName, String attachementName) {
return getAttachment(pageName, attachementName,
getLastAttachmentVersion(pageName, attachementName));
@@ -137,7 +139,7 @@
+ ((attachementName.indexOf(".") != -1) ? tokens[tokens.length - 1]
: "bin"));
- FileInputStream fis;
+ FileInputStream fis = null;
try {
fis = new FileInputStream(file);
return new WikiAttachment(attachementName, new Date(file
@@ -145,6 +147,14 @@
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
+ } finally {
+ try {
+ if (fis != null) {
+ fis.close();
+ }
+ } catch (IOException e) {
+ log.error(e);
+ }
}
}
@@ -189,7 +199,7 @@
if (langCode == null)
langCode = ConfigurationParser.getDefaultLangugeCode();
}
-
+
public synchronized boolean deleteAttachment(String pageName,
String attachmentName) {
@@ -279,9 +289,9 @@
}
public void addLangCode(String langCode, String langDesc) {
-
+
}
-
+
public void addAttachment(InputStream attFile, String attName, String uid,
String user) {
String pageName = uid;
@@ -366,13 +376,13 @@
return attSet;
}
-
+
public void setLanguageCode(String langCode) {
init(langCode);
this.langCode = langCode;
}
-
+
private Properties getAttProps(String pageName, String attName,
boolean createProps) {
Properties attProps = new Properties();
@@ -393,8 +403,10 @@
propFile = new File(pathToAttachments + "/" + pageName + "-att/"
+ attName + "-dir/attachment.properties");
+ FileInputStream fis = null;
try {
- attProps.load(new FileInputStream(propFile));
+ fis = new FileInputStream(propFile);
+ attProps.load(fis);
} catch (IOException ioe) {
System.err
.println("Cannot read attachment.properties for attachement: "
@@ -403,16 +415,28 @@
+ pageName
+ "-att/"
+ attName + "-dir/attachment.properties\n" + ioe);
+ } finally {
+ try {
+ if (fis != null) {
+ fis.close();
+ }
+ } catch (IOException e) {
+ log.error(e);
+ }
}
return attProps;
}
-
+
private void saveAttProps(Properties props, String pageName, String attName) {
+ FileOutputStream fos = null;
+
try {
- props.store(new FileOutputStream(new File(pathToAttachments + "/"
+ fos = new FileOutputStream(new File(pathToAttachments + "/"
+ pageName + "-att/" + attName
- + "-dir/attachment.properties")), FileMediaDataSource.SAVE_COMMENT);
+ + "-dir/attachment.properties"));
+ props.store(fos,
+ FileMediaDataSource.SAVE_COMMENT);
} catch (Exception ioe) {
System.err
.println("Cannot store attchement.properties for attachement: "
@@ -421,7 +445,16 @@
+ pageName
+ "-att/"
+ attName + "-dir/attachments.properties\n" + ioe);
+ } finally {
+ try {
+ if (fos != null) {
+ fos.close();
+ }
+ } catch (IOException e) {
+ log.error(e);
+ }
}
+
}
}
Modified: labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileMediaDataSource.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileMediaDataSource.java 2008-04-03 13:59:50 UTC (rev 19395)
+++ labs/jbosslabs/trunk/portal-extensions/jbosswiki/wiki-common/src/java/org/jboss/wiki/FileMediaDataSource.java 2008-04-03 17:02:16 UTC (rev 19396)
@@ -206,8 +206,8 @@
File pageOldFile = new File(pathToMedia + "/" + uidTranslated
+ ".txt");
- PrintWriter pageFileWriter;
- InputStreamReader pageFileReader;
+ PrintWriter pageFileWriter = null;
+ InputStreamReader pageFileReader = null;
try {
pageFileWriter = new PrintWriter(pageFile, CHARSET);
@@ -241,10 +241,26 @@
throw new WikiSaveException(fnfe);
} catch (IOException ioe) {
throw new WikiSaveException(ioe);
+ } finally {
+ if (pageFileWriter != null) {
+ pageFileWriter.close();
+ }
+
+ try {
+ if (pageFileReader != null) {
+ pageFileReader.close();
+ }
+ } catch (IOException e) {
+ throw new WikiSaveException(e);
+ }
}
} else { // there is no page on disk. We have to create new one.
+ FileOutputStream pageProps = null;
+
+ PrintWriter pageWriter = null;
+
try {
// create needed dirs
@@ -256,7 +272,7 @@
newPageFile = new File(pathToMedia + "/OLD/" + uidTranslated
+ "/page.properties");
- FileOutputStream pageProps = new FileOutputStream(newPageFile);
+ pageProps = new FileOutputStream(newPageFile);
Properties properties = new Properties();
properties.setProperty(page.getLastVersion() + ".author", page
@@ -264,8 +280,6 @@
properties.store(pageProps, SAVE_COMMENT);
- pageProps.close();
-
String pageDirName = pathToMedia;
String[] pageNames = uidTranslated.split("/");
@@ -277,8 +291,8 @@
pageDir.mkdirs();
- PrintWriter pageWriter = new PrintWriter(new File(pathToMedia
- + "/" + uidTranslated + ".txt"), CHARSET);
+ pageWriter = new PrintWriter(new File(pathToMedia + "/"
+ + uidTranslated + ".txt"), CHARSET);
String pageContent = page.getContent();
@@ -293,6 +307,19 @@
} catch (Exception e) {
throw new WikiSaveException(e);
+ } finally {
+
+ try {
+ if (pageProps != null) {
+ pageProps.close();
+ }
+ } catch (IOException e) {
+ throw new WikiSaveException(e);
+ } finally {
+ if (pageWriter != null) {
+ pageWriter.close();
+ }
+ }
}
}
@@ -310,11 +337,12 @@
Properties pageProps = new Properties();
String translatedPageName = FileDSCommons.translateToFileName(pageName);
+ FileInputStream fpi = null;
+
try {
- FileInputStream fpi = new FileInputStream(new File(pathToMedia
- + "/OLD/" + translatedPageName + "/page.properties"));
+ fpi = new FileInputStream(new File(pathToMedia + "/OLD/"
+ + translatedPageName + "/page.properties"));
pageProps.load(fpi);
- fpi.close();
} catch (IOException ioe) {
// There is no prop file for some reson - create one.
@@ -328,25 +356,45 @@
pagePropsFile = new File(pathToMedia + "/OLD/" + translatedPageName
+ "/page.properties");
+ FileOutputStream fpo = null;
+
try {
pagePropsFile.createNewFile();
if (pageExists(pageName)) {
// if page exists it looks like there is lack of the props
// structure
- FileOutputStream fpo = new FileOutputStream(pagePropsFile);
+ fpo = new FileOutputStream(pagePropsFile);
pageProps.setProperty("1.author", "unknown");
pageProps.store(fpo, SAVE_COMMENT);
- fpo.close();
+
}
} catch (IOException e) {
log.error(
"Cannot create page props file for page: " + pageName,
e);
+ } finally {
+ try {
+ if (fpo != null) {
+ fpo.close();
+ }
+ } catch (IOException e) {
+ log.error(e);
+ }
}
+
return pageProps;
+ } finally {
+
+ try {
+ if (fpi != null) {
+ fpi.close();
+ }
+ } catch (IOException e) {
+ log.error(e);
+ }
}
return pageProps;
@@ -383,12 +431,15 @@
} catch (FileNotFoundException e) {
throw new DataSourceException(e);
} catch (IOException e) {
+ throw new DataSourceException(e);
+ } finally {
try {
- fos.close();
+ if (fos != null) {
+ fos.close();
+ }
} catch (IOException e1) {
- throw new DataSourceException(e);
+ throw new DataSourceException(e1);
}
- throw new DataSourceException(e);
}
}
@@ -464,8 +515,10 @@
StringBuilder pageContent = new StringBuilder();
+ InputStreamReader input = null;
+
try {
- InputStreamReader input = new InputStreamReader(
+ input = new InputStreamReader(
new FileInputStream(pageFile), CHARSET);
// FileInputStream input = new FileInputStream(pageFile);
@@ -477,6 +530,14 @@
}
} catch (IOException ioe) {
log.error(ioe);
+ } finally {
+ try {
+ if (input != null) {
+ input.close();
+ }
+ } catch (IOException e) {
+ log.error(e);
+ }
}
Set<String> viewRoles = null;
@@ -576,10 +637,12 @@
+ ".txt");
if (loadContent) {
+ InputStreamReader input = null;
+
try {
- InputStreamReader input = new InputStreamReader(
- new FileInputStream(pageFile), CHARSET);
+ input = new InputStreamReader(new FileInputStream(pageFile),
+ CHARSET);
int nextChr = input.read();
@@ -595,6 +658,14 @@
} catch (IOException ioe) {
log.error(ioe);
+ } finally {
+ try {
+ if (input != null) {
+ input.close();
+ }
+ } catch (IOException e) {
+ log.error(e);
+ }
}
} else {
page.setLength(pageFile.length());
@@ -887,22 +958,20 @@
try {
fis = new FileInputStream(propsFile);
- } catch (FileNotFoundException e1) {
- log.error(e1);
- }
- try {
props.load(fis);
- } catch (IOException e) {
- log.error(e);
+ } catch (IOException e1) {
+ log.error(e1);
+ } finally {
+ try {
+ if (fis != null) {
+ fis.close();
+ }
+ } catch (IOException e) {
+ log.error(e);
+ }
}
- try {
- fis.close();
- } catch (IOException e) {
- log.error(e);
- }
-
return props;
}
@@ -917,21 +986,19 @@
try {
fos = new FileOutputStream(propsFile);
- } catch (FileNotFoundException e) {
- log.error(e);
- }
- try {
props.store(fos, SAVE_COMMENT);
} catch (IOException e) {
log.error(e);
+ } finally {
+ try {
+ if (fos != null) {
+ fos.close();
+ }
+ } catch (IOException e) {
+ log.error(e);
+ }
}
-
- try {
- fos.close();
- } catch (IOException e) {
- log.error(e);
- }
}
public void setLanguageCode(String langCode) {
@@ -945,9 +1012,12 @@
Map<String, String> map = new TreeMap<String, String>();
- map.put(ConfigurationParser.getDefaultLangugeCode(), wikiEngine.getAllLanguageCodes()
- .get(ConfigurationParser.getDefaultLangugeCode()));
+ map.put(ConfigurationParser.getDefaultLangugeCode(), wikiEngine
+ .getAllLanguageCodes().get(
+ ConfigurationParser.getDefaultLangugeCode()));
+ FileInputStream fis = null;
+
try {
File f = new File(pathToMedia + "/" + LANG_FILE);
@@ -955,7 +1025,7 @@
f.createNewFile();
}
- FileInputStream fis = new FileInputStream(f);
+ fis = new FileInputStream(f);
props.load(fis);
@@ -964,6 +1034,14 @@
map.putAll((Map<String, String>) mapd);
} catch (IOException e) {
log.error("Couldn't load properties with language list", e);
+ } finally {
+ if (fis != null) {
+ try {
+ fis.close();
+ } catch (IOException e) {
+ log.error(e);
+ }
+ }
}
return map;
@@ -976,14 +1054,24 @@
props.put(langCode, langDesc);
+ FileOutputStream fos = null;
+
try {
File f = new File(pathToMedia + "/" + LANG_FILE);
- FileOutputStream fos = new FileOutputStream(f);
+ fos = new FileOutputStream(f);
props.store(fos, SAVE_COMMENT);
} catch (IOException e) {
log.error("Couldn't save properties with language list", e);
+ } finally {
+ if (fos != null) {
+ try {
+ fos.close();
+ } catch (IOException e) {
+ log.error(e);
+ }
+ }
}
}
More information about the jboss-svn-commits
mailing list