Skip to content

Commit

Permalink
Merge pull request #461 from danielsoro/FORGE-1836
Browse files Browse the repository at this point in the history
[FORGE-1836] Refactor towards JDK 1.7 enhancements
  • Loading branch information
gastaldi committed May 29, 2014
2 parents 43384d2 + 521ac98 commit 19fd41b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 126 deletions.
55 changes: 18 additions & 37 deletions bootstrap/src/main/java/org/jboss/forge/bootstrap/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,55 +367,36 @@ private void remove(String addonCoordinates)

private static String getServiceName(final ClassLoader classLoader, final String className)
{
final InputStream stream = classLoader.getResourceAsStream("META-INF/services/" + className);
if (stream != null)
try (final InputStream stream = classLoader.getResourceAsStream("META-INF/services/" + className))
{
BufferedReader reader = null;
try
if (stream != null)
{
reader = new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = reader.readLine()) != null)
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream)))
{
final int i = line.indexOf('#');
if (i != -1)
String line;
while ((line = reader.readLine()) != null)
{
line = line.substring(0, i);
final int i = line.indexOf('#');
if (i != -1)
{
line = line.substring(0, i);
}
line = line.trim();
if (line.length() == 0)
continue;
return line;
}
line = line.trim();
if (line.length() == 0)
continue;
return line;
}
}
catch (IOException ignored)
{
// ignore
}
finally
{
try
{
if (reader != null)
reader.close();
}
catch (IOException ignored)
{
// ignore
}

try
{
if (stream != null)
stream.close();
}
catch (IOException e)
{
// ignore
}
}
}
catch (IOException e)
{
// ignore
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package org.jboss.forge.addon.database.tools.connections;

import org.jboss.forge.addon.convert.Converter;
import org.jboss.forge.addon.database.tools.jpa.HibernateDialect;
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIValidationContext;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.input.UISelectOne;
import org.jboss.forge.addon.ui.metadata.WithAttributes;
import org.jboss.forge.addon.ui.validate.UIValidator;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Driver;
Expand All @@ -13,18 +23,6 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.inject.Inject;

import org.jboss.forge.addon.convert.Converter;
import org.jboss.forge.addon.database.tools.jpa.HibernateDialect;
import org.jboss.forge.addon.resource.FileResource;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIValidationContext;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.input.UISelectOne;
import org.jboss.forge.addon.ui.metadata.WithAttributes;
import org.jboss.forge.addon.ui.validate.UIValidator;

public class ConnectionProfileDetailsPage
{

Expand Down Expand Up @@ -70,7 +68,7 @@ public class ConnectionProfileDetailsPage
description = "The class name of the JDBC driver",
required = true)
protected UISelectOne<String> driverClass;

public void initializeUI(UIBuilder builder) throws Exception
{
builder
Expand All @@ -89,85 +87,91 @@ public String convert(HibernateDialect dialect)
}
});
driverLocation.addValidator(new UIValidator()
{
{
@Override
public void validate(UIValidationContext context)
{
FileResource<?> resource = driverLocation.getValue();
if (resource != null && !resource.exists()) {
context.addValidationError(driverLocation, "The location '" + resource.getFullyQualifiedName() + "' does not exist");
if (resource != null && !resource.exists())
{
context.addValidationError(driverLocation, "The location '" + resource.getFullyQualifiedName()
+ "' does not exist");
}
}
});
driverClass.setValueChoices(new Callable<Iterable<String>>() {
driverClass.setValueChoices(new Callable<Iterable<String>>()
{
@Override
public Iterable<String> call() throws Exception
{
return getDriverClassNames();
}
}
});
driverClass.setDefaultValue(new Callable<String>() {
driverClass.setDefaultValue(new Callable<String>()
{
@Override
public String call() throws Exception
{
String result = null;
Iterator<String> iterator = driverClass.getValueChoices().iterator();
if (iterator.hasNext()) {
if (iterator.hasNext())
{
result = iterator.next();
}
return result;
}
}
});

}

private List<String> getDriverClassNames() {

private List<String> getDriverClassNames()
{
ArrayList<String> result = new ArrayList<String>();
FileResource<?> resource = driverLocation.getValue();
if (resource != null && resource.exists()) {
JarFile jarFile = null;
try {
File file = (File)resource.getUnderlyingResourceObject();
File file = (File) resource.getUnderlyingResourceObject();
if (resource != null && resource.exists())
try (JarFile jarFile = new JarFile(file);)
{
URL[] urls = new URL[] { file.toURI().toURL() };
URLClassLoader classLoader = URLClassLoader.newInstance(urls);
Class<?> driverClass = classLoader.loadClass(Driver.class.getName());
jarFile = new JarFile(file);
Enumeration<JarEntry> iter = jarFile.entries();
while (iter.hasMoreElements()) {
while (iter.hasMoreElements())
{
JarEntry entry = iter.nextElement();
if (entry.getName().endsWith(".class")) {
if (entry.getName().endsWith(".class"))
{
String name = entry.getName();
name = name.substring(0, name.length() - 6);
name = name.replace('/', '.');
try {
try
{
Class<?> clazz = classLoader.loadClass(name);
if (driverClass.isAssignableFrom(clazz)) {
if (driverClass.isAssignableFrom(clazz))
{
result.add(clazz.getName());
}
} catch (ClassNotFoundException cnfe) {
//ignore
} catch (NoClassDefFoundError err) {
//ignore
}
catch (ClassNotFoundException cnfe)
{
// ignore
}
catch (NoClassDefFoundError err)
{
// ignore
}
}
}
} catch (Exception e) {
}
catch (Exception e)
{
// ignore and return an empty list
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return result;
}

public void validate(UIValidationContext context)
{
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@

package org.jboss.forge.addon.git.gitignore.resources;

import org.jboss.forge.addon.git.gitignore.GitIgnoreEntry;
import org.jboss.forge.addon.resource.AbstractFileResource;
import org.jboss.forge.addon.resource.Resource;
import org.jboss.forge.addon.resource.ResourceFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.jboss.forge.addon.git.gitignore.GitIgnoreEntry;
import org.jboss.forge.addon.resource.AbstractFileResource;
import org.jboss.forge.addon.resource.Resource;
import org.jboss.forge.addon.resource.ResourceFactory;
import org.jboss.forge.furnace.util.Streams;

/**
* @author Dan Allen
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
Expand Down Expand Up @@ -50,7 +49,7 @@ protected List<Resource<?>> doListResources()
}
return patterns;
}

@Override
public void addPattern(String pattern)
{
Expand Down Expand Up @@ -113,10 +112,8 @@ public List<String> getPatterns()
public List<GitIgnoreEntry> getEntries()
{
List<GitIgnoreEntry> lines = new ArrayList<>();
BufferedReader reader = null;
try
try (BufferedReader reader = new BufferedReader(new InputStreamReader(getResourceInputStream())))
{
reader = new BufferedReader(new InputStreamReader(getResourceInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
{
Expand All @@ -129,10 +126,6 @@ public List<GitIgnoreEntry> getEntries()
throw new RuntimeException(
"Error while reading .gitignore patterns", e);
}
finally
{
Streams.closeQuietly(reader);
}
}

protected void storeEntries(List<GitIgnoreEntry> entries)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.nio.charset.Charset;
import java.util.UUID;

Expand Down Expand Up @@ -105,13 +106,14 @@ public void testFileSize() throws Exception
File file = File.createTempFile("forge", "testFileSize");
file.deleteOnExit();

FileOutputStream fos = new FileOutputStream(file);
Streams.write(new ByteArrayInputStream("Test".getBytes()), fos);
fos.close();
try (FileWriter write = new FileWriter(file))
{
write.append("Test");

FileResource<?> fileResource = factory.create(file).reify(FileResource.class);
FileResource<?> fileResource = factory.create(file).reify(FileResource.class);

Assert.assertEquals(file.length(), fileResource.getSize());
Assert.assertEquals(file.length(), fileResource.getSize());
}
file.delete();
}

Expand Down Expand Up @@ -183,33 +185,33 @@ public void testGetSetContents() throws Exception
File file = File.createTempFile("forge", "testFileReadWrite");
file.deleteOnExit();

FileOutputStream fos = new FileOutputStream(file);
Streams.write(new ByteArrayInputStream("READ".getBytes()), fos);
fos.close();

try (FileWriter write = new FileWriter(file))
{
write.append("READ");
}

FileResource<?> fileResource = factory.create(file).reify(FileResource.class);

Assert.assertEquals("READ", fileResource.getContents());
fileResource.setContents("WRITE");
Assert.assertEquals("WRITE", fileResource.getContents());
file.delete();
}

@Test
public void testGetSetContentsWithCharset() throws Exception
{
File file = File.createTempFile("forge", "testFileReadWrite");
file.deleteOnExit();
Charset charset = Charset.forName("ISO-8859-1");

FileOutputStream fos = new FileOutputStream(file);
Streams.write(new ByteArrayInputStream("READ".getBytes(charset)), fos);
fos.close();

FileResource<?> fileResource = factory.create(file).reify(FileResource.class);
try (FileWriter write = new FileWriter(file))
{
write.append(new String("READ".getBytes(), charset));
}

FileResource<?> fileResource = factory.create(file).reify(FileResource.class);
Assert.assertEquals("READ", fileResource.getContents(charset));
fileResource.setContents("WRITE",charset);
fileResource.setContents("WRITE", charset);
Assert.assertEquals("WRITE", fileResource.getContents(charset));
file.delete();
}
Expand Down
Loading

0 comments on commit 19fd41b

Please sign in to comment.