try with resources
git-svn-id: https://svn.apache.org/repos/asf/archiva/redback/redback-components/trunk@1585690 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/modello-db-keywords/src/main/java/org/apache/archiva/redback/components/modello/db/SQLReservedWords.java b/modello-db-keywords/src/main/java/org/apache/archiva/redback/components/modello/db/SQLReservedWords.java
index 06f4e2f..9328bb6 100644
--- a/modello-db-keywords/src/main/java/org/apache/archiva/redback/components/modello/db/SQLReservedWords.java
+++ b/modello-db-keywords/src/main/java/org/apache/archiva/redback/components/modello/db/SQLReservedWords.java
@@ -41,14 +41,13 @@
* SQLReservedWords - utility object to test against SQL Keywords.
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
- *
* @plexus.component role="org.apache.archiva.redback.components.modello.db.SQLReservedWords"
*/
public class SQLReservedWords
extends AbstractLogEnabled
implements Initializable
{
- private Map<String,List<KeywordSource>> keywords;
+ private Map<String, List<KeywordSource>> keywords;
/**
* Tests the provided word to see if it is a keyword.
@@ -74,7 +73,7 @@
*
* @param word the word to test.
* @return the {@link List} of {@link KeywordSource} objects, or <code>null</code> if specified word is
- * not a reserved word.
+ * not a reserved word.
*/
public List<KeywordSource> getKeywordSourceList( String word )
{
@@ -94,7 +93,7 @@
*
* @param word the wor to test.
* @return the {@link String} of keyword source names seperated by commas, or <code>null</code> if word is
- * not a reserved word.
+ * not a reserved word.
*/
public String getKeywordSourceString( String word )
{
@@ -150,11 +149,8 @@
return;
}
- InputStream is = null;
-
- try
+ try (InputStream is = definitionsURL.openStream())
{
- is = definitionsURL.openStream();
props.load( is );
String sources[] = StringUtils.split( props.getProperty( "keyword.sources" ), "," );
@@ -175,10 +171,6 @@
getLogger().error( "Unable to load definitions file: " + "keywords.properties", e );
return;
}
- finally
- {
- IOUtil.close( is );
- }
}
private void loadKeywordSource( String resource, KeywordSource source )
@@ -191,25 +183,24 @@
return;
}
- InputStream is = null;
- InputStreamReader isr = null;
- BufferedReader reader = null;
- try
+ try (InputStream is = keywordsURL.openStream() )
{
- is = keywordsURL.openStream();
- isr = new InputStreamReader( is );
- reader = new BufferedReader( isr );
-
- String line = reader.readLine();
- while ( line != null )
+ try (InputStreamReader isr = new InputStreamReader( is ))
{
- line = line.trim();
- if ( line.length() > 0 )
+ try (BufferedReader reader = new BufferedReader( isr ))
{
- addKeyword( line, source );
+ String line = reader.readLine();
+ while ( line != null )
+ {
+ line = line.trim();
+ if ( line.length() > 0 )
+ {
+ addKeyword( line, source );
+ }
+ line = reader.readLine();
+ }
}
- line = reader.readLine();
}
}
catch ( IOException e )
@@ -217,12 +208,6 @@
getLogger().warn( "Unable to load keywords from " + keywordsURL.toExternalForm() + ": " + e.getMessage(),
e );
}
- finally
- {
- IOUtil.close( reader );
- IOUtil.close( isr );
- IOUtil.close( is );
- }
}
private void addKeyword( String keyword, KeywordSource source )
diff --git a/modello-plugin-jpox/src/main/java/org/apache/archiva/redback/components/modello/jpox/JPoxJdoMappingModelloGenerator.java b/modello-plugin-jpox/src/main/java/org/apache/archiva/redback/components/modello/jpox/JPoxJdoMappingModelloGenerator.java
index 274a230..1a68e2d 100644
--- a/modello-plugin-jpox/src/main/java/org/apache/archiva/redback/components/modello/jpox/JPoxJdoMappingModelloGenerator.java
+++ b/modello-plugin-jpox/src/main/java/org/apache/archiva/redback/components/modello/jpox/JPoxJdoMappingModelloGenerator.java
@@ -163,7 +163,8 @@
{
throw new ModelloException(
"Error while creating parent directories for the file " + "'" + packageJdo.getAbsolutePath()
- + "'." );
+ + "'."
+ );
}
}
@@ -182,84 +183,92 @@
private void generatePackageJdo( File file, Model model )
throws IOException, ModelloException
{
- OutputStreamWriter fileWriter = new OutputStreamWriter( new FileOutputStream( file ), "UTF-8" );
-
- PrintWriter printWriter = new PrintWriter( fileWriter );
-
- XMLWriter writer = new PrettyPrintXMLWriter( printWriter );
-
- Map<String, List<ModelClass>> classes = new HashMap<>();
-
- for ( Iterator<ModelClass> it = model.getClasses( getGeneratedVersion() ).iterator(); it.hasNext(); )
+ try (FileOutputStream fileOutputStream = new FileOutputStream( file ))
{
- ModelClass modelClass = it.next();
-
- JPoxClassMetadata jpoxMetadata =
- JPoxClassMetadata.class.cast( modelClass.getMetadata( JPoxClassMetadata.ID ) );
-
- if ( !jpoxMetadata.isEnabled() )
+ try (OutputStreamWriter fileWriter = new OutputStreamWriter( fileOutputStream, "UTF-8" ))
{
- // Skip generation of those classes that are not enabled for the jpox plugin.
- continue;
+
+ try (PrintWriter printWriter = new PrintWriter( fileWriter ))
+ {
+
+ XMLWriter writer = new PrettyPrintXMLWriter( printWriter );
+
+ Map<String, List<ModelClass>> classes = new HashMap<>();
+
+ for ( Iterator<ModelClass> it = model.getClasses( getGeneratedVersion() ).iterator();
+ it.hasNext(); )
+ {
+ ModelClass modelClass = it.next();
+
+ JPoxClassMetadata jpoxMetadata =
+ JPoxClassMetadata.class.cast( modelClass.getMetadata( JPoxClassMetadata.ID ) );
+
+ if ( !jpoxMetadata.isEnabled() )
+ {
+ // Skip generation of those classes that are not enabled for the jpox plugin.
+ continue;
+ }
+
+ String packageName = modelClass.getPackageName( isPackageWithVersion(), getGeneratedVersion() );
+
+ List<ModelClass> list = classes.get( packageName );
+
+ if ( list == null )
+ {
+ list = new ArrayList<>();
+ }
+
+ list.add( modelClass );
+
+ classes.put( packageName, list );
+ }
+
+ printWriter.println( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
+ printWriter.println();
+ printWriter.println( "<!DOCTYPE jdo PUBLIC" );
+ printWriter.println( " \"-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 2.0//EN\"" );
+ printWriter.println( " \"http://java.sun.com/dtd/jdo_2_0.dtd\">" );
+ printWriter.println();
+
+ writer.startElement( "jdo" );
+
+ for ( Map.Entry<String, List<ModelClass>> entry : classes.entrySet() )
+ {
+
+ List<ModelClass> list = entry.getValue();
+
+ if ( list.size() == 0 )
+ {
+ continue;
+ }
+
+ String packageName = entry.getKey();
+
+ writer.startElement( "package" );
+
+ writer.addAttribute( "name", packageName );
+
+ for ( ModelClass modelClass : list )
+ {
+ writeClass( writer, modelClass );
+ }
+
+ if ( packageName.equals(
+ model.getDefaultPackageName( isPackageWithVersion(), getGeneratedVersion() ) ) )
+ {
+ writeModelloMetadataClass( writer );
+ }
+
+ writer.endElement(); // package
+ }
+
+ writer.endElement(); // jdo
+
+ printWriter.println();
+
+ }
}
-
- String packageName = modelClass.getPackageName( isPackageWithVersion(), getGeneratedVersion() );
-
- List<ModelClass> list = classes.get( packageName );
-
- if ( list == null )
- {
- list = new ArrayList<>();
- }
-
- list.add( modelClass );
-
- classes.put( packageName, list );
}
-
- printWriter.println( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
- printWriter.println();
- printWriter.println( "<!DOCTYPE jdo PUBLIC" );
- printWriter.println( " \"-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 2.0//EN\"" );
- printWriter.println( " \"http://java.sun.com/dtd/jdo_2_0.dtd\">" );
- printWriter.println();
-
- writer.startElement( "jdo" );
-
- for ( Map.Entry<String, List<ModelClass>> entry : classes.entrySet() )
- {
-
- List<ModelClass> list = entry.getValue();
-
- if ( list.size() == 0 )
- {
- continue;
- }
-
- String packageName = entry.getKey();
-
- writer.startElement( "package" );
-
- writer.addAttribute( "name", packageName );
-
- for ( ModelClass modelClass : list )
- {
- writeClass( writer, modelClass );
- }
-
- if ( packageName.equals( model.getDefaultPackageName( isPackageWithVersion(), getGeneratedVersion() ) ) )
- {
- writeModelloMetadataClass( writer );
- }
-
- writer.endElement(); // package
- }
-
- writer.endElement(); // jdo
-
- printWriter.println();
-
- printWriter.close();
}
private void writeClass( XMLWriter writer, ModelClass modelClass )
@@ -319,7 +328,8 @@
{
throw new ModelloException(
"The JDO mapping generator does not support the specified " + "class identity type '"
- + identityType + "'. " + "Supported types: " + IDENTITY_TYPES );
+ + identityType + "'. " + "Supported types: " + IDENTITY_TYPES
+ );
}
writer.addAttribute( "identity-type", identityType );
}
@@ -1139,7 +1149,8 @@
{
throw new ModelloException(
"The JDO mapping generator does not support the specified " + "value-strategy '" + valueStrategy
- + "'. " + "Supported types: " + VALUE_STRATEGY_LIST );
+ + "'. " + "Supported types: " + VALUE_STRATEGY_LIST
+ );
}
writer.addAttribute( "value-strategy", valueStrategy );
}
@@ -1313,7 +1324,8 @@
{
throw new ModelloException(
"The JDO mapping generator does not support the specified " + "field type '" + modelField.getType()
- + "'. " + "Supported types: " + PRIMITIVE_IDENTITY_MAP.keySet() );
+ + "'. " + "Supported types: " + PRIMITIVE_IDENTITY_MAP.keySet()
+ );
}
}
diff --git a/modello-plugin-store/src/main/java/org/apache/archiva/redback/components/modello/plugin/store/AbstractVelocityModelloGenerator.java b/modello-plugin-store/src/main/java/org/apache/archiva/redback/components/modello/plugin/store/AbstractVelocityModelloGenerator.java
index 20741d7..00443e2 100644
--- a/modello-plugin-store/src/main/java/org/apache/archiva/redback/components/modello/plugin/store/AbstractVelocityModelloGenerator.java
+++ b/modello-plugin-store/src/main/java/org/apache/archiva/redback/components/modello/plugin/store/AbstractVelocityModelloGenerator.java
@@ -148,12 +148,13 @@
try
{
- Writer writer = getEncoding() == null ? WriterFactory.newPlatformWriter( file )
- : WriterFactory.newWriter( file, getEncoding() );
+ try (Writer writer = getEncoding() == null ? WriterFactory.newPlatformWriter( file )
+ : WriterFactory.newWriter( file, getEncoding() ))
+ {
- template.merge( context, writer );
+ template.merge( context, writer );
+ }
- writer.close();
}
catch ( Exception e )
{
diff --git a/modello-plugin-store/src/main/java/org/apache/archiva/redback/components/modello/plugin/store/StoreModelloGenerator.java b/modello-plugin-store/src/main/java/org/apache/archiva/redback/components/modello/plugin/store/StoreModelloGenerator.java
index 58cd565..dc64c10 100644
--- a/modello-plugin-store/src/main/java/org/apache/archiva/redback/components/modello/plugin/store/StoreModelloGenerator.java
+++ b/modello-plugin-store/src/main/java/org/apache/archiva/redback/components/modello/plugin/store/StoreModelloGenerator.java
@@ -101,14 +101,15 @@
{
try
{
- Writer writer = getEncoding() == null ? WriterFactory.newPlatformWriter( file )
- : WriterFactory.newWriter( file, getEncoding() );
+ try (Writer writer = getEncoding() == null ? WriterFactory.newPlatformWriter( file )
+ : WriterFactory.newWriter( file, getEncoding() ))
- velocity.getEngine().mergeTemplate( template, context, writer );
+ {
+ velocity.getEngine().mergeTemplate( template, context, writer );
- writer.flush();
+ writer.flush();
- writer.close();
+ }
}
catch ( Exception e )
{