Skip to content

Commit 3236851

Browse files
committed
Fixed resource loading.
1 parent a099839 commit 3236851

File tree

3 files changed

+68
-111
lines changed

3 files changed

+68
-111
lines changed

src/fbjava-impl/src/main/java/org/firebirdsql/fbjava/impl/BlobConnection.java

Lines changed: 40 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -42,149 +42,83 @@
4242
final class BlobConnection extends URLConnection
4343
{
4444
private static final Logger log = LoggerFactory.getLogger(BlobConnection.class);
45+
private ByteArrayOutputStream bout;
4546

46-
public BlobConnection(URL url)
47+
public BlobConnection(URL url) throws IOException
4748
{
4849
super(url);
50+
51+
connect();
4952
}
5053

5154
@Override
5255
public void connect() throws IOException
5356
{
54-
}
57+
if (bout != null)
58+
return;
5559

56-
@Override
57-
public InputStream getInputStream() throws IOException
58-
{
5960
try
6061
{
6162
final DbClassLoader classLoader = (DbClassLoader) Thread.currentThread().getContextClassLoader();
6263
final Connection conn = classLoader.getConnection();
63-
final PreparedStatement stmt = conn.prepareStatement("select content from sqlj.read_jar(?)");
64-
ResultSet rs = null;
6564

66-
try
65+
try (PreparedStatement stmt = conn.prepareStatement("select content from sqlj.read_jar(?)"))
6766
{
6867
String urlStr = url.toString().substring(8); // "fbjava:/"
6968
stmt.setString(1, urlStr);
70-
final ResultSet finalRs = rs = stmt.executeQuery();
7169

72-
if (rs.next())
70+
try (ResultSet rs = stmt.executeQuery())
7371
{
74-
return new InputStream() {
75-
private InputStream inner = finalRs.getBinaryStream(1);
76-
77-
@Override
78-
public int read() throws IOException
79-
{
80-
return inner.read();
81-
}
82-
83-
@Override
84-
public int read(byte[] b, int off, int len) throws IOException
85-
{
86-
return inner.read(b, off, len);
87-
}
88-
89-
@Override
90-
public long skip(long n) throws IOException
91-
{
92-
return inner.skip(n);
93-
}
94-
95-
@Override
96-
public int available() throws IOException
97-
{
98-
return inner.available();
99-
}
100-
101-
@Override
102-
public void close() throws IOException
103-
{
104-
if (inner == null)
105-
return;
106-
107-
try
108-
{
109-
inner.close();
110-
try
111-
{
112-
finalRs.close();
113-
stmt.close();
114-
}
115-
catch (SQLException e)
116-
{
117-
throw new IOException(e);
118-
}
119-
}
120-
finally
121-
{
122-
inner = null;
123-
}
124-
}
125-
126-
@Override
127-
public void mark(int readlimit)
128-
{
129-
inner.mark(readlimit);
130-
}
131-
132-
@Override
133-
public void reset() throws IOException
134-
{
135-
inner.reset();
136-
}
72+
if (rs.next())
73+
{
74+
InputStream in = rs.getBinaryStream(1);
75+
bout = new ByteArrayOutputStream();
13776

138-
@Override
139-
public boolean markSupported()
140-
{
141-
return inner.markSupported();
142-
}
143-
};
144-
}
145-
else
146-
{
147-
rs.close();
148-
stmt.close();
77+
byte[] buffer = new byte[8192];
78+
int count;
14979

150-
try (PreparedStatement stmt2 = conn.prepareStatement("select child from sqlj.list_dir(?)"))
80+
while ((count = in.read(buffer)) != -1)
81+
bout.write(buffer, 0, count);
82+
}
83+
else
15184
{
152-
stmt2.setString(1, urlStr);
153-
154-
try (ResultSet rs2 = stmt2.executeQuery())
85+
try (PreparedStatement stmt2 = conn.prepareStatement("select child from sqlj.list_dir(?)"))
15586
{
156-
if (rs2.next())
157-
{
158-
ByteArrayOutputStream out = new ByteArrayOutputStream();
159-
PrintWriter writer = new PrintWriter(out);
87+
stmt2.setString(1, urlStr);
16088

161-
do
89+
try (ResultSet rs2 = stmt2.executeQuery())
90+
{
91+
if (rs2.next())
16292
{
163-
writer.println(rs2.getString(1));
164-
} while (rs2.next());
93+
bout = new ByteArrayOutputStream();
94+
PrintWriter writer = new PrintWriter(bout);
16595

166-
writer.close();
96+
do
97+
{
98+
writer.println(rs2.getString(1));
99+
} while (rs2.next());
167100

168-
return new ByteArrayInputStream(out.toByteArray());
101+
writer.flush();
102+
}
169103
}
170104
}
171-
}
172105

173-
throw new IOException(String.format("Resource '%s' has not been found on the database.", url));
106+
throw new IOException(
107+
String.format("Resource '%s' has not been found on the database.", url));
108+
}
174109
}
175110
}
176-
catch (SQLException e)
177-
{
178-
if (rs != null)
179-
rs.close();
180-
stmt.close();
181-
throw e;
182-
}
183111
}
184112
catch (SQLException e)
185113
{
186114
log.error("Error retrieving resource or class from the database", e);
187115
throw new IOException(e);
188116
}
189117
}
118+
119+
@Override
120+
public InputStream getInputStream() throws IOException
121+
{
122+
return new ByteArrayInputStream(bout.toByteArray());
123+
}
190124
}

src/fbjava-impl/src/main/java/org/firebirdsql/fbjava/impl/DbClassLoader.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,21 @@
4242
*/
4343
final class DbClassLoader extends URLClassLoader
4444
{
45+
private final URL contextUrl;
4546
String databaseName;
4647
private FBConnection connection;
4748
CodeSource codeSource;
4849
PermissionCollection codeSourcePermission = new Permissions();
4950

50-
DbClassLoader(String databaseName, URL[] urls, ClassLoader parent)
51+
DbClassLoader(String databaseName, URL contextUrl, ClassLoader parent)
5152
throws SQLException
5253
{
53-
super(urls, parent);
54+
super(new URL[] {contextUrl}, parent);
5455

56+
this.contextUrl = contextUrl;
5557
this.databaseName = databaseName;
5658

57-
codeSource = new CodeSource(urls[0], (Certificate[]) null);
59+
codeSource = new CodeSource(contextUrl, (Certificate[]) null);
5860

5961
Properties properties = new Properties();
6062
properties.setProperty("isc_dpb_no_db_triggers", "1");
@@ -97,4 +99,25 @@ public Connection getConnection()
9799
{
98100
return connection;
99101
}
102+
103+
@Override
104+
public URL getResource(String name)
105+
{
106+
URL url = super.getResource(name);
107+
108+
if (url == null)
109+
{
110+
try
111+
{
112+
url = new URL(contextUrl, name);
113+
url.openConnection();
114+
}
115+
catch (Exception e)
116+
{
117+
url = null;
118+
}
119+
}
120+
121+
return url;
122+
}
100123
}

src/fbjava-impl/src/main/java/org/firebirdsql/fbjava/impl/ExternalEngine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ private static class SharedData
104104

105105
SharedData(String databaseName) throws SQLException, MalformedURLException
106106
{
107-
URL url = new URL(null, "fbjava:/", new URLStreamHandler() {
107+
URL contextUrl = new URL(null, "fbjava:/", new URLStreamHandler() {
108108
@Override
109109
protected URLConnection openConnection(URL url) throws IOException
110110
{
111111
return new BlobConnection(url);
112112
}
113113
});
114114

115-
classLoader = new DbClassLoader(databaseName, new URL[] {url}, getClass().getClassLoader());
115+
classLoader = new DbClassLoader(databaseName, contextUrl, getClass().getClassLoader());
116116
}
117117

118118
void openAttachment(IStatus status, IExternalContext context)

0 commit comments

Comments
 (0)