Author: chathura
Date: Thu Feb 28 22:15:10 2008
New Revision: 14335
Log:
Improved the content streaming of resources.
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/ResourceImpl.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
trunk/registry/modules/core/src/main/resources/database-scripts/derby-complete.sql
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/ResourceImpl.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/ResourceImpl.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/ResourceImpl.java
Thu Feb 28 22:15:10 2008
@@ -19,6 +19,9 @@
package org.wso2.registry;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
import java.sql.Timestamp;
import java.util.*;
import java.io.*;
@@ -35,6 +38,8 @@
*/
public class ResourceImpl implements Resource {
+ private static final Log log = LogFactory.getLog(ResourceImpl.class);
+
/**
* Unique identifier of the resource within the registry. Note that this
is not an UUID.
*/
@@ -138,6 +143,9 @@
*/
private boolean directory = false;
+ /**
+ * Content of the resource, represented as a input stream.
+ */
private InputStream contentStream;
public ResourceImpl() {
@@ -232,11 +240,32 @@
}
public InputStream getContentStream() {
+
+ if (contentStream == null && content != null) {
+
+ if (content instanceof byte[]) {
+ this.contentStream =
+ new BufferedInputStream(new
ByteArrayInputStream((byte[]) content));
+
+ } else if (content instanceof String) {
+ byte[] contentBytes = ((String) content).getBytes();
+ this.contentStream =
+ new BufferedInputStream(new
ByteArrayInputStream(contentBytes));
+ }
+ }
+
return contentStream;
}
public void setContentStream(InputStream contentStream) {
contentModified = true;
+
+ // When a content is set as an input stream, prevoius contents set as
object get invalidated
+ // But we don't want to generate that content till it is explicitly
get using getContent
+ if (contentStream != null) {
+ this.content = null;
+ }
+
this.contentStream = contentStream;
}
@@ -253,25 +282,31 @@
content = out.toByteArray();
} catch (IOException e) {
String msg = "Failed to generate byte array from the resource
content stream.";
+ log.error(msg, e);
throw new RegistryException(msg, e);
} finally {
try {
bufferedInputStream.close();
out.close();
} catch (IOException e) {
- // Do nothing here?
+ log.error("Could not close the input stream of the
content.", e);
}
}
}
+
return content;
}
public void setContent(Object content) {
this.content = content;
- if (content != null && content instanceof byte[]) {
- this.contentStream =
- new BufferedInputStream(new ByteArrayInputStream((byte[])
content));
+
+ // When a new content is set, previous contents set as input stream
get invalid
+ // But we don't want to generate that content till it is explicitly get
+ // using getContentStream
+ if (content != null) {
+ this.contentStream = null;
}
+
this.contentModified = true;
}
@@ -281,7 +316,7 @@
// make sure that the path does not end with a "/"
if (!path.equals(RegistryConstants.ROOT_PATH) &&
- path.endsWith(RegistryConstants.PATH_SEPARATOR)) {
+ path.endsWith(RegistryConstants.PATH_SEPARATOR)) {
preparedPath =
path.substring(0, path.length() -
RegistryConstants.PATH_SEPARATOR.length());
}
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
Thu Feb 28 22:15:10 2008
@@ -1084,6 +1084,10 @@
bufferedIn.close();
fileStream.close();
+ // there is no use of content stream after it is closed. so we can
set it to null to
+ // avoid using it again.
+ resource.setContentStream(null);
+
InputStream tempFileStream = new BufferedInputStream(new
FileInputStream(tempFile));
long contentLength = tempFile.length();
Modified:
trunk/registry/modules/core/src/main/resources/database-scripts/derby-complete.sql
==============================================================================
---
trunk/registry/modules/core/src/main/resources/database-scripts/derby-complete.sql
(original)
+++
trunk/registry/modules/core/src/main/resources/database-scripts/derby-complete.sql
Thu Feb 28 22:15:10 2008
@@ -1,3 +1,4 @@
+
CREATE TABLE ARTIFACTS (AID INTEGER GENERATED ALWAYS AS IDENTITY,
PATH VARCHAR (500) NOT NULL,
MEDIA_TYPE VARCHAR (500),
_______________________________________________
Registry-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/registry-dev