Author: glen
Date: Thu Jan 24 19:40:38 2008
New Revision: 12855
Log:
* Add the ability to take other types aside from byte[] for resource content
(only String for now)
* A little code cleanup, spelling fixes
* Uncomment Keith's test, but he'll be changing this to reflect the actual
problem anyway...
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/VersionHandlingTest.java
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 Jan 24 19:40:38 2008
@@ -19,18 +19,21 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.registry.RegistryConstants;
-import org.wso2.registry.Resource;
import org.wso2.registry.RegistryException;
+import org.wso2.registry.Resource;
import org.wso2.registry.User;
-import org.wso2.registry.utils.AuthorizationUtil;
import org.wso2.registry.jdbc.DatabaseConstants;
+import org.wso2.registry.utils.AuthorizationUtil;
import org.wso2.usermanager.Realm;
+import javax.activation.DataHandler;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
+import java.io.InputStream;
+import java.io.IOException;
public class VersionedResourceDAO {
@@ -152,7 +155,7 @@
}
if (versionNumber == -1) {
- versionNumber = getLatestVerisonNumber(artifactID, conn);
+ versionNumber = getLatestVersionNumber(artifactID, conn);
}
String sqlVersionedFields = "SELECT * FROM VERSIONS WHERE AID=? AND
VN=?";
PreparedStatement versionQuery =
conn.prepareStatement(sqlVersionedFields);
@@ -204,7 +207,8 @@
* @return : Result artifact
* @throws SQLException : if something went wrong
*/
- public Resource getLatestVersion(String path, Connection conn) throws
RegistryException , SQLException {
+ public Resource getLatestVersion(String path, Connection conn)
+ throws RegistryException, SQLException {
/*
* when someone give path like "root/wso2/wsas/r1" , then need to
figure out the latest
* version of the root and from the need to figure out the version of
wso2 and so so
@@ -241,8 +245,8 @@
return null;
}
-// long latestVersionNumber = getLatestVerisonNumber(resource.getId(),
conn);
- long latestVersionNumber = getCurrentVerisonNumber(resource.getPath(),
conn);
+// long latestVersionNumber = getLatestVersionNumber(resource.getId(),
conn);
+ long latestVersionNumber = getCurrentVersionNumber(resource.getPath(),
conn);
String sqlVersionedFields = "SELECT * FROM VERSIONS WHERE AID=? AND
VN=?";
@@ -263,7 +267,7 @@
// get resource children
if (resource.isDirectory()) {
String[] children = getLatestChildPaths(resource.getId(),
-
getCurrentVerisonNumber(path, conn), conn);
+
getCurrentVersionNumber(path, conn), conn);
resource.setContent(children);
}
@@ -295,18 +299,18 @@
* @param conn : Connection to DB
* @return : verion number of the resource user requested
*/
- private long getCurrentVerisonNumber(String path, Connection conn) throws
RegistryException {
+ private long getCurrentVersionNumber(String path, Connection conn) throws
RegistryException {
if (path != null) {
try {
String[] parts = path.split(RegistryConstants.PATH_SEPARATOR);
- if(parts.length > 1 ) {
+ if (parts.length > 1) {
String currentPath = RegistryConstants.ROOT_PATH +
parts[1];
- String parentPath = RegistryConstants.ROOT_PATH;
- long parentVersion = getLatestVerisonNumber(
+ String parentPath = RegistryConstants.ROOT_PATH;
+ long parentVersion = getLatestVersionNumber(
getResourceID(RegistryConstants.ROOT_PATH, conn),
conn);
long childId = 0;
for (int i = 2; i < parts.length; i++) {
- if (!resourceActive(parentPath, conn)){
+ if (!resourceActive(parentPath, conn)) {
throw new RegistryException("Resource is not
active");
}
long parentId = getResourceID(parentPath, conn);
@@ -315,17 +319,17 @@
parentPath = currentPath;
currentPath = currentPath +
RegistryConstants.PATH_SEPARATOR + parts[i];
}
- long id = getResourceID(currentPath,conn);
+ long id = getResourceID(currentPath, conn);
return getChildVersion(conn, childId, parentVersion, id);
} else {
- return getLatestVerisonNumber(getResourceID(path, conn),
conn);
+ return getLatestVersionNumber(getResourceID(path, conn),
conn);
}
// int index =
path.lastIndexOf(RegistryConstants.PATH_SEPARATOR);
// if (index > 0) {
// String parentPath = path.substring(0, index);
// long parentId = getResourceID(parentPath, conn);
-// long latestVersion = getLatestVerisonNumber(parentId,
conn);
+// long latestVersion = getLatestVersionNumber(parentId,
conn);
// long childID = getResourceID(path, conn);
// String SQL = "SELECT DVN FROM DEPENDENCY WHERE AID=? AND
VN=? AND DAID=?";
// PreparedStatement s = conn.prepareStatement(SQL);
@@ -337,7 +341,7 @@
// return result.getLong(1);
// }
// } else {
-// return getLatestVerisonNumber(getResourceID(path, conn),
conn);
+// return getLatestVersionNumber(getResourceID(path, conn),
conn);
// }
} catch (SQLException e) {
log.info("Something went wrong while calculating the current "
+
@@ -349,16 +353,16 @@
}
private long getChildVersion(Connection conn,
- long parentId,
- long parentVersion,
- long childId) throws SQLException {
+ long parentId,
+ long parentVersion,
+ long childId) throws SQLException {
String SQL = "SELECT DVN FROM DEPENDENCY WHERE AID=? AND VN=? AND
DAID=?";
PreparedStatement selectps = conn.prepareStatement(SQL);
selectps.setLong(1, parentId);
selectps.setLong(2, parentVersion);
selectps.setLong(3, childId);
ResultSet childResult = selectps.executeQuery();
- while (childResult.next()) {
+ if (childResult.next()) {
return childResult.getLong("DVN");
}
return -1;
@@ -395,7 +399,7 @@
Connection connection) throws SQLException {
if (versionNumber == -1) {
- versionNumber = getLatestVerisonNumber(resourceID, connection);
+ versionNumber = getLatestVersionNumber(resourceID, connection);
}
if (versionNumber == -1) {
return null;
@@ -454,7 +458,7 @@
Connection connection) throws SQLException {
if (versionNumber == -1) {
- versionNumber = getLatestVerisonNumber(resourceID, connection);
+ versionNumber = getLatestVersionNumber(resourceID, connection);
}
String selectSql = "SELECT DAID, DVN FROM DEPENDENCY WHERE AID=? AND
VN=?";
@@ -498,7 +502,7 @@
String sql =
"INSERT INTO ARTIFACTS (PATH, MEDIA_TYPE, DIRECTORY, STATE,
CREATED_TIME, AUTHOR, " +
- "DESCRIPTION) VALUES (?,?,?,?,?,?,?)";
+ "DESCRIPTION) VALUES (?,?,?,?,?,?,?)";
long now = System.currentTimeMillis();
@@ -556,7 +560,7 @@
s.executeUpdate();
}
- public void markActivated(long id, Connection conn) throws SQLException {
+ public void markActivated(long id, Connection conn) throws SQLException {
String sql = "UPDATE ARTIFACTS SET STATE=? WHERE AID=?";
@@ -614,7 +618,7 @@
return count == 1;
}
- public boolean resourceActive(String path, Connection conn) throws
SQLException {
+ public boolean resourceActive(String path, Connection conn) throws
SQLException {
String sql = "SELECT STATE FROM ARTIFACTS A WHERE A.PATH=?";
@@ -626,11 +630,7 @@
if (result.next()) {
count = result.getLong(1);
}
- if (RegistryConstants.ACTIVE_STATE == count) {
- return true;
- } else {
- return false;
- }
+ return RegistryConstants.ACTIVE_STATE == count;
}
public long getResourceID(String path, Connection conn) throws
SQLException {
@@ -663,27 +663,29 @@
}
/**
- * This method will move a resource from oldpath to newpath , the internal
implementation is such
- * that frit the release will be deleted from oldPath parent and will be
added to the newptah
- * so if the oldpath is "foo/bar/r1" then the r1 will be removed from
foo/bar. However later
- * we can restore and get everything working fine
+ * This method will move a resource from oldpath to newpath , the internal
implementation is
+ * such that frit the release will be deleted from oldPath parent and will
be added to the
+ * newptah so if the oldpath is "foo/bar/r1" then the r1 will be removed
from foo/bar. However
+ * later we can restore and get everything working fine
+ *
* @param oldPath : Current path of the resource
* @param newPath : Where to move the resource
- * @param conn : Connection to DB
- * @throws org.wso2.registry.RegistryException : If something went wrong
- * @param userId : current user
- * @param realm : Realm
+ * @param conn : Connection to DB
+ * @param userId : current user
+ * @param realm : Realm
+ * @throws org.wso2.registry.RegistryException
+ * : If something went wrong
*/
public void renameResource(String oldPath,
String newPath,
- Connection conn ,
+ Connection conn,
String userId,
Realm realm) throws RegistryException {
try {
Resource resource = getLatestVersion(oldPath, conn);
String resourcePath = resource.getPath();
long resourceID = resource.getId();
- //to see whether the patent node is there in the table , if not
need to add that
+ //to see whether the patent node is there in the table , if not
need to add that
createParentCollections(newPath, conn, userId, realm);
// if the resource is a directory then need to populate the
dependecy table with
// the new resource id , while keeping the old one as it is
@@ -692,7 +694,7 @@
} else {
// Then its just need to add the resource and no need to
update any of the
// dependecy tables
- resource.setPath(newPath);
+ resource.setPath(newPath);
if (resource.getAuthorUserName() == null) {
resource.setAuthorUserName(userId);
}
@@ -714,7 +716,8 @@
}
}
- public long addResourceVersion(Resource resource, Connection connection)
throws SQLException , RegistryException {
+ public long addResourceVersion(Resource resource, Connection connection)
+ throws SQLException, RegistryException {
if (resource.getLastModified() == null) {
resource.setLastModified(new java.util.Date());
@@ -731,14 +734,14 @@
throws SQLException {
String sql =
- "INSERT INTO VERSIONS (AID, VN ,CONTENT , AUTHOR,
UPDATED_TIME) VALUES (?,?,?,?,?)";
+ "INSERT INTO VERSIONS (AID, VN, CONTENT, AUTHOR, UPDATED_TIME)
VALUES (?,?,?,?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setLong(1, resource.getId());
// Need to get the old version and increment that
// Select VID from latest table ,
// if it is there then find from that , if it is not there then we
need ti start from 0
- long versionNumber = getLatestVerisonNumber(resource.getId(),
connection);
+ long versionNumber = getLatestVersionNumber(resource.getId(),
connection);
if (versionNumber == -1) {
versionNumber = 1;
} else {
@@ -748,11 +751,8 @@
statement.setLong(2, versionNumber);
Object content = resource.getContent();
- if (content != null && content instanceof byte[]) {
- statement.setBytes(3, (byte[])content);
- } else {
- statement.setBytes(3, null);
- }
+ byte [] bytes = convertToBytes(content);
+ statement.setBytes(3, bytes);
statement.setString(4, resource.getLastUpdaterUserName());
statement.setTimestamp(5, new Timestamp(System.currentTimeMillis()));
@@ -761,9 +761,39 @@
return versionNumber;
}
+ public static byte[] convertToBytes(Object object) {
+ if (object instanceof byte[])
+ return (byte[])object;
+ if (object instanceof String)
+ return ((String)object).getBytes();
+ if (object instanceof DataHandler) {
+ try {
+ object = ((DataHandler)object).getInputStream();
+ } catch (IOException e) {
+ return null;
+ }
+ }
+ if (object instanceof InputStream) {
+ // TODO - fix this to really work.
+ InputStream is = (InputStream)object;
+ byte[] bytes;
+ try {
+ int avail = is.available();
+ bytes = new byte[avail];
+ is.read(bytes);
+ } catch (IOException e) {
+ return null;
+ }
+ return bytes;
+ }
+
+ return null;
+ }
+
private void updateDependency(Resource resource,
long versionNumber,
- boolean delete, Connection connection)
throws SQLException , RegistryException {
+ boolean delete, Connection connection)
+ throws SQLException, RegistryException {
// updating dependency table , so that we can rollback to a particular
version
// updating for the current resource
@@ -810,7 +840,8 @@
*/
private long updateDependencyTable(Resource resource,
long versionNumber,
- boolean delete, Connection connection)
throws SQLException , RegistryException {
+ boolean delete, Connection connection)
+ throws SQLException, RegistryException {
/**
* D0
* D1 D2
@@ -843,10 +874,11 @@
if (resource.getLastModified() == null) {
resource.setLastModified(new java.util.Date());
}
+
// Need to get the parent's parent and update the and update
the
- long artifactversionNumber =
getCurrentVerisonNumber(parentPath, connection);
-// long artifactversionNumber =
getLatestVerisonNumber(parentId, connection);
+ long artifactVersionNumber =
getCurrentVersionNumber(parentPath, connection);
+// long artifactVersionNumber =
getLatestVersionNumber(parentId, connection);
long nextVersionNumber = createNextVersion(parentId,
resource.getLastUpdaterUserName(),
@@ -856,7 +888,7 @@
String selectSql = "SELECT DAID, DVN FROM DEPENDENCY WHERE
AID=? AND VN=?";
PreparedStatement s = connection.prepareStatement(selectSql);
s.setLong(1, parentId);
- s.setLong(2, artifactversionNumber);
+ s.setLong(2, artifactVersionNumber);
ResultSet result = s.executeQuery();
ArrayList dependeyMap = new ArrayList();
@@ -885,8 +917,8 @@
dependeyMap.add(ds);
}
- for (int i = 0; i < dependeyMap.size(); i++) {
- PreparedStatement preparedStatement =
(PreparedStatement)dependeyMap.get(i);
+ for (Object aDependeyMap : dependeyMap) {
+ PreparedStatement preparedStatement =
(PreparedStatement)aDependeyMap;
preparedStatement.executeUpdate();
}
@@ -901,7 +933,7 @@
return -1;
}
- public long getLatestVerisonNumber(long resourceId, Connection connection)
throws SQLException {
+ public long getLatestVersionNumber(long resourceId, Connection connection)
throws SQLException {
String sql = "SELECT MAX(VN) FROM VERSIONS WHERE AID=?";
PreparedStatement s = connection.prepareStatement(sql);
@@ -929,7 +961,7 @@
}
public void restore(long resourceId, long toVersion, Connection connection)
- throws SQLException , RegistryException {
+ throws SQLException, RegistryException {
Resource oldArtifact = getResourceByID(resourceId, toVersion,
connection);
if (User.getCurrentUser() != null) {
@@ -944,7 +976,7 @@
addResourceVersion(oldArtifact, connection);
if (oldArtifact.isDirectory()) {
- activateDirectory(oldArtifact,connection);
+ activateDirectory(oldArtifact, connection);
String selectChildSql = "SELECT DAID, DVN FROM DEPENDENCY WHERE
AID=? AND VN=?";
PreparedStatement selectps =
connection.prepareStatement(selectChildSql);
selectps.setLong(1, resourceId);
@@ -954,33 +986,33 @@
while (childResult.next()) {
long daid = childResult.getLong("DAID");
- markActivated(daid,connection);
+ markActivated(daid, connection);
long dvn = childResult.getLong("DVN");
- long versionNumber = getLatestVerisonNumber(resourceId,
connection);
+ long versionNumber = getLatestVersionNumber(resourceId,
connection);
PreparedStatement ps =
getStatementForDependencyTable(connection,
resourceId, versionNumber,
daid,
dvn);
dependencyList.add(ps);
}
- for (int i = 0; i < dependencyList.size(); i++) {
- PreparedStatement preparedStatement =
(PreparedStatement)dependencyList.get(i);
+ for (Object aDependencyList : dependencyList) {
+ PreparedStatement preparedStatement =
(PreparedStatement)aDependencyList;
preparedStatement.executeUpdate();
}
}
}
/**
- * To delete a resource , what actually happen here is that update the
version table with the new
- * data and resource will not actually removed from the table
+ * To delete a resource , what actually happen here is that update the
version table with the
+ * new data and resource will not actually removed from the table
*
* @param resource : Resource to be deleted
* @param connection : Connection to the DB
* @throws java.sql.SQLException : if something went wrong
*/
public void deleteResource(Resource resource,
- Connection connection) throws SQLException ,
RegistryException{
- long versionNumber = getLatestVerisonNumber(resource.getId(),
connection);
+ Connection connection) throws SQLException,
RegistryException {
+ long versionNumber = getLatestVersionNumber(resource.getId(),
connection);
updateDependency(resource, versionNumber, true, connection);
}
@@ -989,7 +1021,7 @@
long lastModifiedTime,
Connection connection) throws SQLException {
- long maxVersion = getLatestVerisonNumber(resourceID, connection);
+ long maxVersion = getLatestVersionNumber(resourceID, connection);
if (maxVersion == -1) {
maxVersion = 0;
}
@@ -1023,9 +1055,9 @@
/**
- * This method is use to see whether a give resource is active or not ,
one can delete a resource
- * and try to access that giving the path of the resource in that case we
should not return the
- * resource if it is deleted.
+ * This method is use to see whether a give resource is active or not ,
one can delete a
+ * resource and try to access that giving the path of the resource in that
case we should not
+ * return the resource if it is deleted.
*
* @param resourcePath : Path of the resource
* @param connection : Connection to DB
@@ -1096,16 +1128,17 @@
* for example if the path is "/abc/xyx/r1" , then first it will see
whether the resource "/abc"
* is there if it is not there then it will create that else go to
"/abc/xyz" , and see whether
* the resource is there , if so continue the same procedure
- * @param path : Location where we need to add the resource
- * @param conn : DB connection
+ *
+ * @param path : Location where we need to add the resource
+ * @param conn : DB connection
* @param userID : user
- * @throws SQLException : Something went wrong
+ * @param realm : Realm
+ * @throws SQLException : Something went wrong
* @throws RegistryException : Something went wrong
- * @param realm : Realm
*/
private void createParentCollections(String path,
Connection conn,
- String userID ,
+ String userID,
Realm realm) throws SQLException,
RegistryException {
String[] parts = path.split(RegistryConstants.PATH_SEPARATOR);
@@ -1125,21 +1158,22 @@
}
}
- private void deleteDirectory(Resource collection, Connection conn) throws
SQLException , RegistryException {
- List childList = getChildren(collection.getId(), -1, conn);
- Iterator i = childList.iterator();
- while (i.hasNext()) {
- Resource child = (Resource)i.next();
- if (child.isDirectory()) {
- deleteDirectory(child, conn);
- } else {
- markDeleted(child.getPath(), conn);
- deleteResource(child, conn);
- }
- }
- markDeleted(collection.getPath(), conn);
- deleteResource(collection, conn);
- }
+// private void deleteDirectory(Resource collection, Connection conn)
+// throws SQLException, RegistryException {
+// List childList = getChildren(collection.getId(), -1, conn);
+// Iterator i = childList.iterator();
+// while (i.hasNext()) {
+// Resource child = (Resource)i.next();
+// if (child.isDirectory()) {
+// deleteDirectory(child, conn);
+// } else {
+// markDeleted(child.getPath(), conn);
+// deleteResource(child, conn);
+// }
+// }
+// markDeleted(collection.getPath(), conn);
+// deleteResource(collection, conn);
+// }
private void activateDirectory(Resource collection, Connection conn)
throws SQLException {
List childList = getChildren(collection.getId(), -1, conn);
@@ -1154,5 +1188,5 @@
}
markActivated(collection.getId(), conn);
}
-
+
}
Modified:
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/VersionHandlingTest.java
==============================================================================
---
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/VersionHandlingTest.java
(original)
+++
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/VersionHandlingTest.java
Thu Jan 24 19:40:38 2008
@@ -94,23 +94,20 @@
}
-// public void testResourceVersioning() throws Exception{
-//
-// String mashupString = "testMashup";
-//
-// Resource mashup = new Resource();
-// mashup.setContent(mashupString);
-// registry.put("/wso2/mashup", mashup);
-//
-//
-// Resource wsas = new Resource();
-// wsas.setContent("testWSAS".getBytes());
-// registry.put("/wso2/wsas", wsas);
-//
-//
-// Resource myMashup = registry.get("/wso2/mashup");
-// String content = (String) myMashup.getContent();
-// assertNotNull(content);
-// assertEquals(mashupString,content);
-// }
+ public void testResourceVersioning() throws Exception{
+ String mashupString = "testMashup";
+
+ Resource mashup = new Resource();
+ mashup.setContent(mashupString);
+ registry.put("/wso2/mashup", mashup);
+
+ Resource wsas = new Resource();
+ wsas.setContent("testWSAS".getBytes());
+ registry.put("/wso2/wsas", wsas);
+
+ Resource myMashup = registry.get("/wso2/mashup");
+ String content = new String((byte[])myMashup.getContent());
+ assertNotNull(content);
+ assertEquals(mashupString,content);
+ }
}
_______________________________________________
Registry-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/registry-dev