Author: glen
Date: Mon Dec 17 12:23:07 2007
New Revision: 11296
Log:
Code cleanup.
Formatting, and a few changes such as introducing a getConnection() method to
wrap repeated code in JDBCRegistry.
Both these files now light up green in IDEA.
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
Mon Dec 17 12:23:07 2007
@@ -44,8 +44,8 @@
* JDBC based implementation of the Registry. This will be used mostly as the
back-end by other
* Registry implementations. This can use either an in-memory database or a
external database
* configured using data source. At the initializing, it checks if a data
source named
- * "jdbc/WSO2RegistryDB" is configured. If such data source is found, it is
used as the database.
- * If a data source is not found, an in-memory database is created using the
HSQL.
+ * "jdbc/WSO2RegistryDB" is configured. If such data source is found, it is
used as the database. If
+ * a data source is not found, an in-memory database is created using the HSQL.
*/
public class JDBCRegistry implements Registry {
@@ -66,7 +66,8 @@
* Default constructor. JDBC registry should be configured using the
configure(...) method, if
* it is instantiated using this contructor.
*/
- public JDBCRegistry() {}
+ public JDBCRegistry() {
+ }
/**
* Contructs a JDBC registry to be used without any security
(authorizations on actions). JDBC
@@ -74,7 +75,6 @@
* instances.
*
* @param dataSource Data source to be used to store resources and
metadata.
- *
* @throws RegistryException
*/
public JDBCRegistry(DataSource dataSource) throws RegistryException {
@@ -87,12 +87,11 @@
* applied on the resources created using this registry.
*
* @param dataSource Data source to be used to store resources and
metadata. Note that the same
- * data source may or may not be used as the data source of user manager.
- *
- * @param realm User manager realm to handle authorizations. It is
strongly recommented to
- * use a realm instance contructed using the RegistryRealmFactory. All
initail users, roles
- * and authorizations will be set on realms obtained using the
RegistryRealmFactory.
- *
+ * data source may or may not be used as the data source
of user manager.
+ * @param realm User manager realm to handle authorizations. It is
strongly recommented to
+ * use a realm instance contructed using the
RegistryRealmFactory. All initail
+ * users, roles and authorizations will be set on realms
obtained using the
+ * RegistryRealmFactory.
* @throws RegistryException
*/
public JDBCRegistry(DataSource dataSource, Realm realm) throws
RegistryException {
@@ -104,8 +103,8 @@
* Configures and initiates the JDBC registry with a (new) datasource and
a realm. This is
* useful for changing underlying databases at run-time.
*
- * @param dataSource
- * @param realm
+ * @param dataSource the DataSource to use
+ * @param realm Realm to use
* @throws RegistryException
*/
public void configure(DataSource dataSource, RegistryRealm realm) throws
RegistryException {
@@ -127,12 +126,7 @@
logsDAO = new LogsDAO();
// check if root is added. if not add it.
- Connection conn = null;
- try {
- conn = dataSource.getConnection();
- } catch (SQLException e) {
- throw new RegistryException(e.getMessage());
- }
+ Connection conn = getConnection();
try {
conn.setAutoCommit(false);
@@ -146,7 +140,9 @@
root.setLastUpdaterUserName(RegistryConstants.SYSTEM_USER);
resourceDAO.add(root, conn);
- AuthorizationUtil.setDefaultAuthorizations(defaultRealm,
RegistryConstants.ROOT_PATH, RegistryConstants.SYSTEM_USER);
+ AuthorizationUtil.setDefaultAuthorizations(defaultRealm,
+
RegistryConstants.ROOT_PATH,
+
RegistryConstants.SYSTEM_USER);
root =
resourceDAO.getLatestVersion(RegistryConstants.ROOT_PATH, conn);
resourceDAO.addResourceVersion(root, conn);
@@ -219,11 +215,9 @@
boolean resourceExist = false;
- Connection conn = null;
+ Connection conn = getConnection();
try {
- conn = dataSource.getConnection();
resourceExist = resourceDAO.resourceExist(path, conn);
-
} catch (SQLException e) {
String msg = "Failed to check the exsitence of a resource at path
" + path;
log.error(msg, e);
@@ -231,7 +225,7 @@
} finally {
try {
- conn.close();
+ if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
@@ -243,10 +237,10 @@
/**
* Adds or updates resources in the registry. If there is no resource in
the given path,
* resource is added. If a resource already exist in the given path, it
will be replaced with
- * the new resource. Implementations may use a concurrency control method
to prevent
- * concurrency issues.
+ * the new resource. Implementations may use a concurrency control method
to prevent concurrency
+ * issues.
*
- * @param path Path of the new resource
+ * @param path Path of the new resource
* @param resource Resource instance for the new resource
* @throws org.wso2.registry.RegistryException
* : if the user is not authorized
@@ -255,10 +249,9 @@
path = preparePath(path);
mediaTypeManager.put(path, resource);
- Connection conn = null;
+ Connection conn = getConnection();
try {
- conn = dataSource.getConnection();
conn.setAutoCommit(false);
long resourceID = resourceDAO.getResourceID(path, conn);
@@ -300,10 +293,9 @@
mediaTypeManager.delete(path);
- Connection conn = null;
+ Connection conn = getConnection();
try {
- conn = dataSource.getConnection();
conn.setAutoCommit(false);
long resourceID = resourceDAO.getResourceID(path, conn);
@@ -326,7 +318,8 @@
try {
conn.close();
} catch (SQLException ignore) {
- log.info("Exception while closing the DB connection in delte
method of JDBCRegistry");
+ log.info(
+ "Exception while closing the DB connection in delte
method of JDBCRegistry");
}
}
}
@@ -335,10 +328,9 @@
String[] versionPaths = null;
- Connection conn = null;
+ Connection conn = getConnection();
try {
- conn = dataSource.getConnection();
long artifactID = resourceDAO.getResourceID(path, conn);
String[] versionIDs = resourceDAO.getVersionNumbers(artifactID,
conn);
@@ -356,7 +348,7 @@
}
String msg = "Could not get the version paths of the resource " +
- path + ". Caused by: " + e.getMessage();
+ path + ". Caused by: " + e.getMessage();
log.error(msg, e);
throw new RegistryException(msg);
@@ -371,8 +363,18 @@
return versionPaths;
}
+ private Connection getConnection() throws RegistryException {
+ Connection conn;
+ try {
+ conn = dataSource.getConnection();
+ } catch (SQLException e) {
+ throw new RegistryException("Couldn't open db connection", e);
+ }
+ return conn;
+ }
+
public void restoreVersion(String versionPath) throws RegistryException {
- String plainPath ;
+ String plainPath;
long versionNumber;
if (versionPath.indexOf("?v=") != -1) {
String[] parts = versionPath.split("\\?v=");
@@ -384,10 +386,9 @@
throw new RegistryException(msg);
}
- Connection conn = null;
+ Connection conn = getConnection();
try {
- conn = dataSource.getConnection();
conn.setAutoCommit(false);
long artifactID = resourceDAO.getResourceID(plainPath, conn);
@@ -401,7 +402,7 @@
} catch (SQLException e) {
String msg = "Could not restore the version " +
- versionPath + ". Caused by: " + e.getMessage();
+ versionPath + ". Caused by: " + e.getMessage();
log.error(msg);
try {
@@ -417,7 +418,7 @@
conn.close();
} catch (SQLException ignore) {
log.info("Exception while closing the DB connection in " +
- "restoreVersion method of JDBCRegistry");
+ "restoreVersion method of JDBCRegistry");
}
}
}
@@ -427,8 +428,8 @@
////////////////////////////////////////////////////////
/**
- * Applies the given tag to the resource in the given path. If the given
tag is not defined
- * in the registry, it will be defined.
+ * Applies the given tag to the resource in the given path. If the given
tag is not defined in
+ * the registry, it will be defined.
*
* @param resourcePath Path of the resource to be tagged.
* @param tag Tag. Any string can be used for the tag.
@@ -439,7 +440,7 @@
resourcePath = preparePath(resourcePath);
- Connection conn = null;
+ Connection conn = getConnection();
String userID = User.getCurrentUser();
@@ -447,7 +448,6 @@
String[] tags = tag.split(",");
try {
- conn = dataSource.getConnection();
conn.setAutoCommit(false);
if (!resourceDAO.resourceExist(resourcePath, conn)) {
@@ -466,7 +466,8 @@
if (tagsDAO.taggingExist(tags[i], resource.getId(), userID,
conn)) {
String msg =
- Messages.getMessage("resource.already.tagged",
resourcePath, tags[i], userID);
+ Messages.getMessage("resource.already.tagged",
resourcePath, tags[i],
+ userID);
log.error(msg);
throw new RegistryException(msg);
}
@@ -515,10 +516,8 @@
//List pathList = new ArrayList();
List taggedPaths = new ArrayList();
- Connection conn = null;
+ Connection conn = getConnection();
try {
- conn = dataSource.getConnection();
-
List pathList = tagsDAO.getPathsWithAnyTag(tags, conn);
Iterator iPaths = pathList.iterator();
while (iPaths.hasNext()) {
@@ -527,9 +526,9 @@
TaggedResourcePath taggedResourcePath = new
TaggedResourcePath();
taggedResourcePath.setResourcePath(path);
- for (int i = 0; i < tags.length; i++) {
- long count = tagsDAO.getTagCount(path, tags[i], conn);
- taggedResourcePath.addTagCount(tags[i], count);
+ for (String tag1 : tags) {
+ long count = tagsDAO.getTagCount(path, tag1, conn);
+ taggedResourcePath.addTagCount(tag1, count);
}
taggedPaths.add(taggedResourcePath);
@@ -550,7 +549,8 @@
}
return
- (TaggedResourcePath[]) taggedPaths.toArray(new
TaggedResourcePath[taggedPaths.size()]);
+ (TaggedResourcePath[]) taggedPaths
+ .toArray(new TaggedResourcePath[taggedPaths.size()]);
}
/**
@@ -565,14 +565,12 @@
resourcePath = preparePath(resourcePath);
- Connection conn = null;
+ Connection conn = getConnection();
Tag[] tags = null;
try {
- conn = dataSource.getConnection();
tags = tagsDAO.getTagsWithCount(resourcePath, conn);
-
} catch (SQLException e) {
String msg = Messages.getMessage("tag.search.fail.2",
resourcePath);
@@ -596,15 +594,14 @@
String user = User.getCurrentUser();
- Connection conn = null;
+ Connection conn = getConnection();
try {
- conn = dataSource.getConnection();
conn.setAutoCommit(false);
Resource taggedResource = resourceDAO.getLatestVersion(path, conn);
if (user.equals(taggedResource.getAuthorUserName())) {
- tagsDAO.removeTag(path, tag, conn);
+ tagsDAO.removeTag(path, tag, conn);
} else {
tagsDAO.removeTag(path, tag, user, conn);
}
@@ -623,7 +620,8 @@
try {
conn.close();
} catch (SQLException ignore) {
- log.info("Exception while closing the DB connection in
removeTag method of JDBCRegistry");
+ log.info(
+ "Exception while closing the DB connection in
removeTag method of JDBCRegistry");
}
}
}
@@ -645,12 +643,11 @@
resourcePath = preparePath(resourcePath);
- Connection conn = null;
+ Connection conn = getConnection();
String userID = User.getCurrentUser();
try {
- conn = dataSource.getConnection();
conn.setAutoCommit(false);
Resource resource = resourceDAO.getLatestVersion(resourcePath,
conn);
@@ -701,12 +698,11 @@
resourcePath = preparePath(resourcePath);
- Connection conn = null;
+ Connection conn = getConnection();
Comment[] comments = new Comment[0];
try {
- conn = dataSource.getConnection();
comments = commentsDAO.getComments(resourcePath, conn);
} catch (SQLException e) {
@@ -737,16 +733,16 @@
* @throws RegistryException ResourceNotFound exception is thrown if a
resource does not exist
* in the given path.
*/
- public synchronized void rateResource(String resourcePath, int rating)
throws RegistryException {
+ public synchronized void rateResource(String resourcePath, int rating)
+ throws RegistryException {
resourcePath = preparePath(resourcePath);
- Connection conn = null;
+ Connection conn = getConnection();
String userID = User.getCurrentUser();
try {
- conn = dataSource.getConnection();
conn.setAutoCommit(false);
Resource resource = resourceDAO.getLatestVersion(resourcePath,
conn);
@@ -755,13 +751,13 @@
if (ratingsDAO.ratingExist(resource.getId(), userID, conn)) {
ratingsDAO.updateRating(resource.getId(), userID, rating,
conn);
log.info("Updated the rating on the resource " +
- resourcePath + " by user " + userID);
+ resourcePath + " by user " + userID);
} else {
ratingsDAO.addRating(resource.getId(), userID, rating,
conn);
}
logsDAO.addLog(resource.getId(),
- userID, LogEntry.RATING, Integer.toString(rating),
conn);
+ userID, LogEntry.RATING,
Integer.toString(rating), conn);
} else {
String msg = Messages.getMessage("rate.on.null.artfact",
resourcePath);
@@ -806,12 +802,11 @@
resourcePath = preparePath(resourcePath);
- Connection conn = null;
+ Connection conn = getConnection();
float rating = 0;
try {
- conn = dataSource.getConnection();
rating = ratingsDAO.getAverageRating(resourcePath, conn);
} catch (SQLException e) {
@@ -834,17 +829,16 @@
path = preparePath(path);
- Connection conn = null;
+ Connection conn = getConnection();
int rating = 0;
try {
- conn = dataSource.getConnection();
rating = ratingsDAO.getRating(path, userName, conn);
} catch (SQLException e) {
String msg = "Could not get the rating of the resource " + path +
- " given by the user " + userName + ". Caused by: " +
e.getMessage();
+ " given by the user " + userName + ". Caused by: " +
e.getMessage();
log.error(msg, e);
throw new RegistryException(msg);
@@ -863,20 +857,21 @@
// Extensible searching API
//////////////////////////////////////////////////////////////////////////////
- public synchronized void defineQuery (String name, String query) throws
RegistryException {
+ public synchronized void defineQuery(String name, String query) throws
RegistryException {
- Connection conn = null;
+ Connection conn = getConnection();
String userID = User.getCurrentUser();
// make the path of the query (e.g. /users/chathura/queries/myquery1 )
String queryPath = RegistryConstants.ROOT_PATH +
RegistryConstants.USERS_COLLECTION_NAME +
- RegistryConstants.PATH_SEPARATOR + userID +
RegistryConstants.PATH_SEPARATOR +
- RegistryConstants.QUERIES_COLLECTION_NAME +
RegistryConstants.PATH_SEPARATOR + name;
+ RegistryConstants.PATH_SEPARATOR + userID +
RegistryConstants
+ .PATH_SEPARATOR +
+ RegistryConstants.QUERIES_COLLECTION_NAME +
RegistryConstants
+ .PATH_SEPARATOR + name;
try {
- conn = dataSource.getConnection();
conn.setAutoCommit(false);
Resource queryResource = new Resource();
@@ -918,17 +913,11 @@
}
- public Resource executeQuery (String path, Object[] parameters) throws
RegistryException {
+ public Resource executeQuery(String path, Object[] parameters) throws
RegistryException {
- Connection conn = null;
- try {
- conn = dataSource.getConnection();
- } catch (SQLException e) {
- throw new RegistryException(e.getMessage());
- }
+ Connection conn = getConnection();
try {
-
Resource query = get(path);
QueryProcessor queryProcessor =
new
QueryProcessorFactory().getQueryProcessor(query.getMediaType());
@@ -940,7 +929,8 @@
try {
conn.close();
} catch (SQLException ignore) {
- log.info("Exception while closing the DB connection in
executeQuery method of JDBCRegistry");
+ log.info(
+ "Exception while closing the DB connection in
executeQuery method of JDBCRegistry");
}
}
}
@@ -966,12 +956,11 @@
String userName,
Date from, Date to, boolean recentFirst) throws
RegistryException {
- Connection conn = null;
+ Connection conn = getConnection();
List logEntryList;
try {
- conn = dataSource.getConnection();
logEntryList =
logsDAO.getLogs(resourcePath, action, userName, from, to,
recentFirst, conn);
@@ -984,7 +973,8 @@
try {
conn.close();
} catch (SQLException ignore) {
- log.info("Exception while closing the DB connection in getLogs
method of JDBCRegistry");
+ log.info(
+ "Exception while closing the DB connection in getLogs
method of JDBCRegistry");
}
}
LogEntry[] logEntries = new LogEntry[logEntryList.size()];
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
Mon Dec 17 12:23:07 2007
@@ -38,18 +38,11 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
-/**
- * Servlet for providing REST API for the registry.
- */
+/** Servlet for providing REST API for the registry. */
public class RegistryServlet extends HttpServlet {
private static final Log log = LogFactory.getLog(RegistryServlet.class);
protected transient ServletConfig servletConfig;
- /**
- * Core registry used in the back-end. Usually this is a JDBCRegistry and
there is only one
- * instance of this registry for the whole application. This should be
initialized in this
- * servlet and put in to the servlet context so that it can be accessed
from any where.
- */
private ConsoleURLMapper consoleURLMapper;
//To store the conetxt path
@@ -61,9 +54,10 @@
Registry coreRegistry = null;
try {
registryRealm = new InMemoryRegistryRealm();
- String coreRegistryTypeParam =
config.getInitParameter(RegistryConstants.REGISTRY_TYPE_PARAMTER);
+ String coreRegistryTypeParam =
+
config.getInitParameter(RegistryConstants.REGISTRY_TYPE_PARAMTER);
if (coreRegistryTypeParam == null ||
-
coreRegistryTypeParam.equals(RegistryConstants.JDBC_REGISTRY_TYPE)) {
+
coreRegistryTypeParam.equals(RegistryConstants.JDBC_REGISTRY_TYPE)) {
coreRegistry = new InMemoryJDBCRegistry(registryRealm);
} else if
(coreRegistryTypeParam.equals(RegistryConstants.REMOTE_REGISTRY_TYPE)) {
// todo: initialize core registry with a remote registry
instance.
@@ -71,12 +65,13 @@
// create a system registry and put it in the context
SecureRegistry systemRegistry =
new SecureRegistry(RegistryConstants.SYSTEM_USER,
- RegistryConstants.SYSTEM_PASSWORD,
- coreRegistry,
- registryRealm);
+ RegistryConstants.SYSTEM_PASSWORD,
+ coreRegistry,
+ registryRealm);
config.getServletContext().setAttribute(RegistryConstants.REGISTRY,
coreRegistry);
-
config.getServletContext().setAttribute(RegistryConstants.REGISTRY_REALM,
registryRealm);
-
System.getProperties().put(RegistryConstants.REGISTRY,coreRegistry);
+ config.getServletContext()
+ .setAttribute(RegistryConstants.REGISTRY_REALM,
registryRealm);
+ System.getProperties().put(RegistryConstants.REGISTRY,
coreRegistry);
System.getProperties().put(RegistryConstants.SYSTEM_REGISTRY,
systemRegistry);
} catch (RegistryException e) {
@@ -114,7 +109,8 @@
String path = fileElement.getPath();
registry.put(path, fileElement);
request.getSession().setAttribute(RegistryConstants.STATUS_MESSAGE_NAME,
- "Resource " + path + " is sucessfully added to the
registry.");
+ "Resource " + path +
+ " is sucessfully added to the
registry.");
response.setContentType("text/html");
request.getRequestDispatcher(ConsoleURLMapper.RESOURCES_JSP)
.forward(request, response);
@@ -123,7 +119,7 @@
e.printStackTrace();
request.getSession().setAttribute(RegistryConstants.STATUS_MESSAGE_NAME,
- e.getMessage());
+ e.getMessage());
response.setContentType("text/html");
request.getRequestDispatcher(ConsoleURLMapper.RESOURCES_JSP)
.forward(request, response);
@@ -199,7 +195,8 @@
.include(request, response);
return true;
}
- String path = uri.substring((contextRoot +
RegistryConstants.TAG_REGISTRY).length(), uri.length());
+ String path = uri.substring((contextRoot +
RegistryConstants.TAG_REGISTRY).length(),
+ uri.length());
path = path + query;
try {
@@ -211,7 +208,7 @@
if (content != null) {
if (content instanceof byte[]) {
response.setContentType(resource.getMediaType());
- response.getOutputStream().write((byte[])content);
+ response.getOutputStream().write((byte[]) content);
response.flushBuffer();
} else {
response.setContentType(resource.getMediaType());
_______________________________________________
Registry-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/registry-dev