Author: keith Date: Thu Apr 24 19:16:10 2008 New Revision: 16124 Log:
Pulling out initRegistry method into a separate class. Next step is to refractor the new class into smaller manageable methods MASHUP-771 Added: trunk/mashup/java/modules/core/src/org/wso2/mashup/RegistryInitializer.java Modified: trunk/mashup/java/modules/javascriptdeployer/src/org/wso2/mashup/deployer/JSDeployer.java Added: trunk/mashup/java/modules/core/src/org/wso2/mashup/RegistryInitializer.java ============================================================================== --- (empty file) +++ trunk/mashup/java/modules/core/src/org/wso2/mashup/RegistryInitializer.java Thu Apr 24 19:16:10 2008 @@ -0,0 +1,466 @@ +/* + * Copyright 2008,2009 WSO2, Inc. http://www.wso2.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wso2.mashup; + +import org.apache.axis2.AxisFault; +import org.apache.axis2.context.ConfigurationContext; +import org.wso2.mashup.realm.wsas.WSASRegistryRealm; +import org.wso2.mashup.utils.MashupUtils; +import org.wso2.mashup.webapp.utils.RegistryUtils; +import org.wso2.registry.ActionConstants; +import org.wso2.registry.Collection; +import org.wso2.registry.Registry; +import org.wso2.registry.RegistryConstants; +import org.wso2.registry.RegistryException; +import org.wso2.registry.ResourceImpl; +import org.wso2.registry.jdbc.EmbeddedRegistry; +import org.wso2.registry.jdbc.utils.RegistryDataSource; +import org.wso2.registry.session.UserRegistry; +import org.wso2.registry.users.AccessControlAdmin; +import org.wso2.registry.users.UserRealm; +import org.wso2.registry.users.UserStoreAdmin; +import org.wso2.registry.users.UserStoreException; +import org.wso2.registry.users.accesscontrol.AccessControlConstants; +import org.wso2.registry.users.def.DefaultRealmConfig; +import org.wso2.utils.ServerConfiguration; +import org.wso2.wsas.admin.service.UserAdmin; + +public class RegistryInitializer { + + public static void initRegistry(ConfigurationContext configCtx) throws MashupFault { + + final String SYSTEM_USER_NAME = "System"; + final String SYSTEM_USER_BIO = "System User"; + final String SAMPLES_USER_NAME = "Samples"; + final String SAMPLES_USER_BIO = "Samples User"; + final String ANNONYMOUS_USER_NAME = "Visitor"; + final String ANNONYMOUS_USER_BIO = "Annonymous User"; + final String ADMIN_USER_BIO = "System Administrator"; + final String NONE = "none"; + + // flag used to decide weather the creation of a primary account is needed or not + boolean createPrimary; + String primaryName = null; + String primaryPassword; + UserRegistry systemRegistry; + + ServerConfiguration serverConfig = ServerConfiguration.getInstance(); + + // Get the database connection details for the registry database from the server.xml + String registryDriverClass = serverConfig + .getFirstProperty(MashupConstants.REGISTRY_CONFIG + MashupConstants.DOT + + MashupConstants.DATABASE_CONNECTION + MashupConstants.DOT + + MashupConstants.DRIVER_CLASS); + String registryUrl = serverConfig + .getFirstProperty(MashupConstants.REGISTRY_CONFIG + MashupConstants.DOT + + MashupConstants.DATABASE_CONNECTION + MashupConstants.DOT + + MashupConstants.URL); + String registryUsername = serverConfig + .getFirstProperty(MashupConstants.REGISTRY_CONFIG + MashupConstants.DOT + + MashupConstants.DATABASE_CONNECTION + MashupConstants.DOT + + MashupConstants.USERNAME); + String registryPassword = serverConfig + .getFirstProperty(MashupConstants.REGISTRY_CONFIG + MashupConstants.DOT + + MashupConstants.DATABASE_CONNECTION + MashupConstants.DOT + + MashupConstants.PASSWORD); + + if (registryDriverClass == null || "".equals(registryDriverClass) || + registryUrl == null || "".equals(registryUrl)) { + throw new MashupFault("Cannot find DataBase connection details pertaining to " + + "the registry. The driverClass and the url are mandatory."); + } + + // Get the database connection details for the usermanager database from the server.xml + String usermanagerDriverClass = serverConfig + .getFirstProperty( + MashupConstants.USERMANAGER_CONFIG + MashupConstants.DOT + MashupConstants + .DATABASE_CONNECTION + MashupConstants.DOT + + MashupConstants.DRIVER_CLASS); + String usermanagerUrl = serverConfig + .getFirstProperty(MashupConstants.USERMANAGER_CONFIG + MashupConstants.DOT + + MashupConstants.DATABASE_CONNECTION + MashupConstants.DOT + + MashupConstants.URL); + String usermanagerUsername = serverConfig + .getFirstProperty(MashupConstants.USERMANAGER_CONFIG + MashupConstants.DOT + + MashupConstants.DATABASE_CONNECTION + MashupConstants.DOT + + MashupConstants.USERNAME); + String usermanagerPassword = serverConfig + .getFirstProperty(MashupConstants.USERMANAGER_CONFIG + MashupConstants.DOT + + MashupConstants.DATABASE_CONNECTION + MashupConstants.DOT + + MashupConstants.PASSWORD); + + if (usermanagerDriverClass == null || "".equals(usermanagerDriverClass) || + usermanagerUrl == null || "".equals(usermanagerUrl)) { + throw new MashupFault("Cannot find DataBase connection details pertaining to " + + "the usermanager. The driverClass and the url are mandatory."); + } + + try { + + // We need to create a new role called mashup_user so that all users of the mashup + // server can be added to this role. We need this to set the defaults permissions for + // users of the server + UserAdmin admin = new UserAdmin(); + boolean hasMashupUserRole = false; + String[] roleNames = admin.getRoleNames(); + for (int i = 0; i < roleNames.length; i++) { + if (MashupConstants.MASHUP_USER_ROLE.equals(roleNames[i])) { + hasMashupUserRole = true; + break; + } + } + if (!hasMashupUserRole) { + admin.addRole(MashupConstants.MASHUP_USER_ROLE, + MashupConstants.MASHUP_USER_ROLE_NAME); + } + + // We create a new WSASRegistryRealm here. The WSASRegistryRealm is specially designed + // so that we have all the authentication details in the WSAS database while we maintain + // the authorization details in a stripped down version of the usermanager database + UserRealm realm = new WSASRegistryRealm(); + DefaultRealmConfig config = (DefaultRealmConfig) realm.getRealmConfiguration(); + + config.setConnectionURL(usermanagerUrl); + config.setDriverName(usermanagerDriverClass); + config.setConnectionUserName(usermanagerUsername); + config.setConnectionPassword(usermanagerPassword); + realm.init(config); + + UserStoreAdmin us = realm.getUserStoreAdmin(); + + // We use this as a check to check weather the regiatry was initialized. We dont need to + // execute these stuff each time the server is restarted. We need it only on a first + // time start. + + // Add a set of users and roles that is needed by the Mashup Server + if (!us.isExistingUser(RegistryConstants.ANONYMOUS_USER)) { + + // Add a user called annonymous. All users not signed into the server are in this + // role. Hence permissions assigned to these users are the default permissions set + // for guests. + us.addUser(RegistryConstants.ANONYMOUS_USER, RegistryConstants.ANONYMOUS_PASSWORD); + + // Add a user called system. The system user is managed by the mashup server and all + // server management tasks are performed using this user account + us.addUser(RegistryConstants.SYSTEM_USER, RegistryConstants.SYSTEM_PASSWORD); + + // Add a user called samples. The sample user is managed by the mashup server and + // all samples are deployed using this account + us.addUser(MashupConstants.SAMPLES_USER, MashupConstants.SAMPLES_USER_PASSWORD); + + // Add an admin role so that admins can be put into this role + us.addRole(RegistryConstants.ADMIN_ROLE); + + // Add an everyone role so that all users are added to this role + us.addRole(RegistryConstants.EVERYONE_ROLE); + + // Assign system user the 'admin' role. + us.addUserToRole(MashupConstants.SYSTEM_USER, RegistryConstants.ADMIN_ROLE); + + AccessControlAdmin ac = realm.getAccessControlAdmin(); + + // Setting permissions for the admin role on user resources + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.USER_RESOURCE, + AccessControlConstants.ADD); + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.USER_RESOURCE, + AccessControlConstants.READ); + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.USER_RESOURCE, + AccessControlConstants.EDIT); + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.USER_RESOURCE, + AccessControlConstants.DELETE); + + // Setting permissions for the admin role on role resources + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.ROLE_RESOURCE, + AccessControlConstants.ADD); + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.ROLE_RESOURCE, + AccessControlConstants.READ); + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.ROLE_RESOURCE, + AccessControlConstants.EDIT); + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.ROLE_RESOURCE, + AccessControlConstants.DELETE); + + // Setting permissions for the admin role on setting permissions + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, + AccessControlConstants.USER_PERMISSION_RESOURCE, + AccessControlConstants.READ); + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, + AccessControlConstants.USER_PERMISSION_RESOURCE, + AccessControlConstants.ADD); + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, + AccessControlConstants.USER_PERMISSION_RESOURCE, + AccessControlConstants.EDIT); + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, + AccessControlConstants.USER_PERMISSION_RESOURCE, + AccessControlConstants.DELETE); + + // Setting permissions for the admin role to GET, PUT and DELETE data from any part + // of the registry + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, + ActionConstants.GET); + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, + ActionConstants.PUT); + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, + ActionConstants.DELETE); + ac.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, + AccessControlConstants.AUTHORIZE); + + // Setting permissions for the everyone role to GET data from any part + // of the registry + ac.authorizeRole(RegistryConstants.EVERYONE_ROLE, RegistryConstants.ROOT_PATH, + ActionConstants.GET); + ac.authorizeRole(RegistryConstants.EVERYONE_ROLE, RegistryConstants.ROOT_PATH, + ActionConstants.GET); + + // Setting permissions for the everyone role to READ and EDIT user resources + ac.authorizeRole(RegistryConstants.EVERYONE_ROLE, + AccessControlConstants.USER_RESOURCE, AccessControlConstants.READ); + ac.authorizeRole(RegistryConstants.EVERYONE_ROLE, + AccessControlConstants.USER_RESOURCE, AccessControlConstants.EDIT); + + // Setting permissions for the everyone role to READ permissions + ac.authorizeRole(RegistryConstants.EVERYONE_ROLE, + AccessControlConstants.ROLE_RESOURCE, AccessControlConstants.READ); + ac.authorizeRole(RegistryConstants.EVERYONE_ROLE, + AccessControlConstants.USER_PERMISSION_RESOURCE, + AccessControlConstants.READ); + + // Setting permissions for the everyone role to READ all mashups and queries + ac.authorizeRole(MashupConstants.EVERYONE_ROLE, MashupConstants.ALL_MASHUPS_PATH, + AccessControlConstants.READ); + ac.authorizeRole(MashupConstants.EVERYONE_ROLE, + MashupConstants.ALL_MASHUPS_QUERY_PATH, + AccessControlConstants.READ); + } + + createPrimary = Boolean.parseBoolean( + serverConfig.getFirstProperty(MashupConstants.PRIMARY_USER_CONFIG + "." + + MashupConstants.CREATE_PRIMARY_USER)); + + // If the server.xml contains primary account details and if this is the first startup + // then create the primary account + if (createPrimary && !MashupUtils.isInitialSetupComplete()) { + primaryName = + serverConfig.getFirstProperty(MashupConstants.PRIMARY_USER_CONFIG + "." + + MashupConstants.PRIMARY_USER); + primaryPassword = + serverConfig.getFirstProperty(MashupConstants.PRIMARY_USER_CONFIG + "." + + MashupConstants.PRIMARY_PASSWORD); + us.addUser(primaryName, primaryPassword); + } + + RegistryDataSource datasource = new RegistryDataSource(registryUrl, registryDriverClass, + registryUsername, + registryPassword); + + EmbeddedRegistry embeddedRegistry = new EmbeddedRegistry(datasource, realm); + + // Add the realm and registry as parameters in the AxisConfiguration so that they can be + // used later on as an when needed + configCtx.getAxisConfiguration() + .addParameter(RegistryConstants.REGISTRY, embeddedRegistry); + configCtx.getAxisConfiguration().addParameter(RegistryConstants.REGISTRY_REALM, realm); + + // Instantiating a secure registry using the system account, in order to create user + // profiles. + systemRegistry = embeddedRegistry.getSystemRegistry(); + + if (!systemRegistry.resourceExists(MashupConstants.QUERYSTORE_QUERY_PATH)) { + + // Create an area in the registry to add queries + Collection queryCollection = systemRegistry.newCollection(); + systemRegistry.put(MashupConstants.QUERYSTORE_QUERY_PATH, queryCollection); + + // All mashups query + addQuery( + systemRegistry, + MashupConstants.ALL_MASHUPS_QUERY_PATH, + "SELECT PATH FROM RESOURCE WHERE (MEDIA_TYPE='" + + MashupConstants.MASHUP_MEDIA_TYPE + "' OR " + + "MEDIA_TYPE='wso2_data_service') ORDER BY LAST_UPDATED_TIME DESC", + RegistryConstants.RESOURCES_RESULT_TYPE + ); + + // Mashups from a specific user + addQuery( + systemRegistry, + MashupConstants.MY_MASHUPS_QUERY_PATH, + "SELECT PATH FROM RESOURCE WHERE CREATOR=? AND " + + "(MEDIA_TYPE='" + MashupConstants.MASHUP_MEDIA_TYPE + "' OR " + + "MEDIA_TYPE='wso2_data_service') ORDER BY LAST_UPDATED_TIME DESC", + RegistryConstants.RESOURCES_RESULT_TYPE + ); + + // Mashups rated 4.0 or better, sorted by rating. + addQuery( + systemRegistry, + MashupConstants.TOP_MASHUPS_QUERY_PATH, + "SELECT A.PATH FROM RESOURCE A, RATING R WHERE " + + "A.AID=R.AID AND (A.MEDIA_TYPE='" + + MashupConstants.MASHUP_MEDIA_TYPE + "' OR " + + "A.MEDIA_TYPE='wso2_data_service') GROUP BY A.PATH HAVING " + + "AVG(R.RATING) >= 4.0 ORDER BY AVG(R.RATING) DESC", + RegistryConstants.RESOURCES_RESULT_TYPE + ); + + // Mashups from a specific user rated 4.0 or better, sorted by rating. + addQuery( + systemRegistry, + MashupConstants.MY_TOP_MASHUPS_QUERY_PATH, + "SELECT A.PATH FROM RESOURCE A, RATING R WHERE " + + "A.AID=R.AID AND A.CREATOR=? AND (A.MEDIA_TYPE='" + + MashupConstants.MASHUP_MEDIA_TYPE + "' OR " + + "A.MEDIA_TYPE='wso2_data_service') GROUP BY A.PATH HAVING " + + "AVG(R.RATING) >= 4.0 ORDER BY AVG(R.RATING) DESC", + RegistryConstants.RESOURCES_RESULT_TYPE + ); + + // Create a collection for users. All user related data will go in here + Collection usersCollection = systemRegistry.newCollection(); + systemRegistry.put(MashupConstants.USERS_PATH, usersCollection); + + // Create a collection for profiles. All user profiles data will go in here + Collection profileCollection = systemRegistry.newCollection(); + systemRegistry.put(MashupConstants.PROFILES_PATH, profileCollection); + + // Create a collection for system. All system related data will go in here + Collection systemCollection = systemRegistry.newCollection(); + systemRegistry.put(MashupConstants.SYSTEM_PATH, systemCollection); + + // Create a collection for /system/queries. All system queries will go in here + Collection queriesCollection = systemRegistry.newCollection(); + systemRegistry.put(MashupConstants.SYSTEM__QUERIES_PATH, queriesCollection); + + // Create a collection for mashups. All mashup related data will go in here + Collection mashupsCollection = systemRegistry.newCollection(); + systemRegistry.put(MashupConstants.ALL_MASHUPS_PATH, mashupsCollection); + + // This query accepts a resource path, a username, and a tag name, and returns the + // resource path if that user applied that tag to the resource. + // Used for checking whether the current user should be allowed to delete the tag. + addQuery( + systemRegistry, + MashupConstants.USER_TAGS_QUERY_PATH, + "SELECT A.PATH FROM RESOURCE A, TAG T WHERE A.PATH=? " + + "AND T.TAG_NAME=? AND T.RID=A.RID AND T.USER_ID=? AND " + + "(A.MEDIA_TYPE='" + MashupConstants.MASHUP_MEDIA_TYPE + + "' OR A.MEDIA_TYPE='wso2_data_service')", + RegistryConstants.RESOURCES_RESULT_TYPE + ); + + // Query for mashups based on tag, description, or title + // todo: queries aren't case insensitive + addQuery( + systemRegistry, + MashupConstants.CUSTOM_QUERY_PATH, + "SELECT DISTINCT A.PATH FROM RESOURCE A, TAG T WHERE " + + "(A.PATH LIKE ? OR A.DESCRIPTION LIKE ? OR (T.TAG_NAME=? AND " + + "T.RID=A.RID)) AND (A.MEDIA_TYPE='" + MashupConstants + .MASHUP_MEDIA_TYPE + + "' OR A.MEDIA_TYPE='wso2_data_service')", + RegistryConstants.RESOURCES_RESULT_TYPE + ); + + // Query for mashups from a specific user based on tag, description, or title + // todo: queries aren't case insensitive + addQuery( + systemRegistry, + MashupConstants.MY_CUSTOM_QUERY_PATH, + "SELECT DISTINCT A.PATH FROM RESOURCE A, TAG T WHERE " + + "(A.PATH LIKE ? OR A.DESCRIPTION LIKE ? OR (T.TAG_NAME=? AND " + + "T.RID=A.RID)) AND A.CREATOR=? AND (A.MEDIA_TYPE='" + + MashupConstants.MASHUP_MEDIA_TYPE + + "' OR A.MEDIA_TYPE='wso2_data_service')", + RegistryConstants.RESOURCES_RESULT_TYPE + ); + + // Query for comments + addQuery( + systemRegistry, + MashupConstants.COMMENTS_QUERY_PATH, + "SELECT C.CM_ID FROM COMMENT C, RESOURCE A WHERE C.COMMENT_TEXT " + + "LIKE ? AND C.RID=A.RID AND (A.MEDIA_TYPE='" + + MashupConstants.MASHUP_MEDIA_TYPE + + "' OR A.MEDIA_TYPE='wso2_data_service')", + RegistryConstants.COMMENTS_RESULT_TYPE + ); + + // Query for comments by a particular user + addQuery( + systemRegistry, + MashupConstants.MY_COMMENTS_QUERY_PATH, + "SELECT C.CM_ID FROM COMMENT C, RESOURCE A WHERE C.COMMENT_TEXT " + + "LIKE ? AND C.USER_ID=? AND C.RID=A.RID AND " + + "(A.MEDIA_TYPE='" + MashupConstants.MASHUP_MEDIA_TYPE + + "' OR A.MEDIA_TYPE='wso2_data_service')", + RegistryConstants.COMMENTS_RESULT_TYPE + ); + + // Query for all comments by a particular user + addQuery( + systemRegistry, + MashupConstants.USERS_COMMENTS_QUERY_PATH, + "SELECT C.CM_ID FROM COMMENT C, RESOURCE A WHERE C.USER_ID=? AND " + + "C.RID=A.RID AND AND (A.MEDIA_TYPE='" + + MashupConstants.MASHUP_MEDIA_TYPE + + "' OR A.MEDIA_TYPE='wso2_data_service')", + RegistryConstants.COMMENTS_RESULT_TYPE + ); + + // Create the anonymous user profile. + RegistryUtils.createUser(systemRegistry, + RegistryConstants.ANONYMOUS_USER, ANNONYMOUS_USER_NAME, + NONE, + ANNONYMOUS_USER_BIO); + + // Create the system user profile. + RegistryUtils.createUser(systemRegistry, MashupConstants.SYSTEM_USER, + SYSTEM_USER_NAME, NONE, SYSTEM_USER_BIO); + + // Create the samples user and profile. + RegistryUtils.createUser(systemRegistry, MashupConstants.SAMPLES_USER, + SAMPLES_USER_NAME, NONE, SAMPLES_USER_BIO); + } + + // If primary user has not been created, do so. + if (createPrimary && !MashupUtils.isInitialSetupComplete()) { + //Create the admin profile using information from a config file. + RegistryUtils + .createUser(systemRegistry, primaryName, primaryName, NONE, + ADMIN_USER_BIO); + // Assign system user the 'admin' role and make primary. + us.addUserToRole(primaryName, RegistryConstants.ADMIN_ROLE); + RegistryUtils.makeUserPrimary(realm, primaryName); + MashupUtils.setInitialSetupComplete(true); + } + } catch (UserStoreException e) { + throw new MashupFault(e); + } catch (RegistryException e) { + throw new MashupFault(e); + } catch (AxisFault axisFault) { + throw new MashupFault(axisFault); + } + } + + private static void addQuery(Registry registry, String path, String sql, String type) + throws RegistryException { + ResourceImpl q = new ResourceImpl(); + q.setContent(sql); + q.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE); + q.setProperty(RegistryConstants.RESULT_TYPE_PROPERTY_NAME, type); + q.setPath(path); + registry.put(path, q); + } +} \ No newline at end of file Modified: trunk/mashup/java/modules/javascriptdeployer/src/org/wso2/mashup/deployer/JSDeployer.java ============================================================================== --- trunk/mashup/java/modules/javascriptdeployer/src/org/wso2/mashup/deployer/JSDeployer.java (original) +++ trunk/mashup/java/modules/javascriptdeployer/src/org/wso2/mashup/deployer/JSDeployer.java Thu Apr 24 19:16:10 2008 @@ -59,27 +59,14 @@ import org.wso2.javascript.rhino.JavaScriptReceiver; import org.wso2.mashup.MashupConstants; import org.wso2.mashup.MashupFault; -import org.wso2.mashup.realm.wsas.WSASRegistryRealm; +import org.wso2.mashup.RegistryInitializer; import org.wso2.mashup.utils.MashupUtils; -import org.wso2.mashup.webapp.utils.RegistryUtils; -import org.wso2.registry.ActionConstants; -import org.wso2.registry.Collection; -import org.wso2.registry.Registry; import org.wso2.registry.RegistryConstants; import org.wso2.registry.RegistryException; -import org.wso2.registry.ResourceImpl; import org.wso2.registry.jdbc.EmbeddedRegistry; -import org.wso2.registry.jdbc.utils.RegistryDataSource; import org.wso2.registry.session.UserRegistry; -import org.wso2.registry.users.AccessControlAdmin; import org.wso2.registry.users.UserRealm; -import org.wso2.registry.users.UserStoreAdmin; -import org.wso2.registry.users.UserStoreException; -import org.wso2.registry.users.accesscontrol.AccessControlConstants; -import org.wso2.registry.users.def.DefaultRealmConfig; -import org.wso2.utils.ServerConfiguration; import org.wso2.wsas.ServerConstants; -import org.wso2.wsas.admin.service.UserAdmin; import javax.xml.namespace.QName; import java.io.BufferedReader; @@ -133,14 +120,6 @@ private final String HTTP_TRANSPORT = "http"; private final String HTTPS_TRANSPORT = "https"; - private final String SYSTEM_USER_NAME = "System"; - private final String SYSTEM_USER_BIO = "System User"; - private final String SAMPLES_USER_NAME = "Samples"; - private final String SAMPLES_USER_BIO = "Samples User"; - private final String ANNONYMOUS_USER_NAME = "Visitor"; - private final String ANNONYMOUS_USER_BIO = "Annonymous User"; - private final String ADMIN_USER_BIO = "System Administrator"; - private final String NONE = "none"; /** * Initialize the deployer. @@ -165,7 +144,7 @@ // We initialize the registry in here cause this is the first point that the // Mashup Server gets control before JavaScript Services are deployed. // We prepare the registry for mashups to be added into it. - initRegistry(); + RegistryInitializer.initRegistry(configCtx); } catch (MashupFault mashupFault) { throw new RuntimeException(mashupFault); } catch (SchedulerException e) { @@ -921,418 +900,4 @@ axisConfig.getPhasesInfo().setOperationPhases(axisOp); } } - - private void initRegistry() throws MashupFault { - - // flag used to decide weather the creation of a primary account is needed or not - boolean createPrimary; - String primaryName = null; - String primaryPassword; - UserRegistry systemRegistry; - - ServerConfiguration serverConfig = ServerConfiguration.getInstance(); - - // Get the database connection details for the registry database from the server.xml - String registryDriverClass = serverConfig - .getFirstProperty(MashupConstants.REGISTRY_CONFIG + MashupConstants.DOT + - MashupConstants.DATABASE_CONNECTION + MashupConstants.DOT + - MashupConstants.DRIVER_CLASS); - String registryUrl = serverConfig - .getFirstProperty(MashupConstants.REGISTRY_CONFIG + MashupConstants.DOT + - MashupConstants.DATABASE_CONNECTION + MashupConstants.DOT + - MashupConstants.URL); - String registryUsername = serverConfig - .getFirstProperty(MashupConstants.REGISTRY_CONFIG + MashupConstants.DOT + - MashupConstants.DATABASE_CONNECTION + MashupConstants.DOT + - MashupConstants.USERNAME); - String registryPassword = serverConfig - .getFirstProperty(MashupConstants.REGISTRY_CONFIG + MashupConstants.DOT + - MashupConstants.DATABASE_CONNECTION + MashupConstants.DOT + - MashupConstants.PASSWORD); - - if (registryDriverClass == null || "".equals(registryDriverClass) || - registryUrl == null || "".equals(registryUrl)) { - throw new MashupFault("Cannot find DataBase connection details pertaining to " + - "the registry. The driverClass and the url are mandatory."); - } - - // Get the database connection details for the usermanager database from the server.xml - String usermanagerDriverClass = serverConfig - .getFirstProperty( - MashupConstants.USERMANAGER_CONFIG + MashupConstants.DOT + MashupConstants - .DATABASE_CONNECTION + MashupConstants.DOT + - MashupConstants.DRIVER_CLASS); - String usermanagerUrl = serverConfig - .getFirstProperty(MashupConstants.USERMANAGER_CONFIG + MashupConstants.DOT + - MashupConstants.DATABASE_CONNECTION + MashupConstants.DOT + - MashupConstants.URL); - String usermanagerUsername = serverConfig - .getFirstProperty(MashupConstants.USERMANAGER_CONFIG + MashupConstants.DOT + - MashupConstants.DATABASE_CONNECTION + MashupConstants.DOT + - MashupConstants.USERNAME); - String usermanagerPassword = serverConfig - .getFirstProperty(MashupConstants.USERMANAGER_CONFIG + MashupConstants.DOT + - MashupConstants.DATABASE_CONNECTION + MashupConstants.DOT + - MashupConstants.PASSWORD); - - if (usermanagerDriverClass == null || "".equals(usermanagerDriverClass) || - usermanagerUrl == null || "".equals(usermanagerUrl)) { - throw new MashupFault("Cannot find DataBase connection details pertaining to " + - "the usermanager. The driverClass and the url are mandatory."); - } - - try { - - // We need to create a new role called mashup_user so that all users of the mashup - // server can be added to this role. We need this to set the defaults permissions for - // users of the server - UserAdmin admin = new UserAdmin(); - boolean hasMashupUserRole = false; - String[] roleNames = admin.getRoleNames(); - for (int i = 0; i < roleNames.length; i++) { - if (MashupConstants.MASHUP_USER_ROLE.equals(roleNames[i])) { - hasMashupUserRole = true; - break; - } - } - if (!hasMashupUserRole) { - admin.addRole(MashupConstants.MASHUP_USER_ROLE, - MashupConstants.MASHUP_USER_ROLE_NAME); - } - - // We create a new WSASRegistryRealm here. The WSASRegistryRealm is specially designed - // so that we have all the authentication details in the WSAS database while we maintain - // the authorization details in a stripped down version of the usermanager database - UserRealm realm = new WSASRegistryRealm(); - DefaultRealmConfig config = (DefaultRealmConfig) realm.getRealmConfiguration(); - - config.setConnectionURL(usermanagerUrl); - config.setDriverName(usermanagerDriverClass); - config.setConnectionUserName(usermanagerUsername); - config.setConnectionPassword(usermanagerPassword); - realm.init(config); - - UserStoreAdmin us = realm.getUserStoreAdmin(); - - // We use this as a check to check weather the regiatry was initialized. We dont need to - // execute these stuff each time the server is restarted. We need it only on a first - // time start. - - // Add a set of users and roles that is needed by the Mashup Server - if (!us.isExistingUser(RegistryConstants.ANONYMOUS_USER)) { - - // Add a user called annonymous. All users not signed into the server are in this - // role. Hence permissions assigned to these users are the default permissions set - // for guests. - us.addUser(RegistryConstants.ANONYMOUS_USER, RegistryConstants.ANONYMOUS_PASSWORD); - - // Add a user called system. The system user is managed by the mashup server and all - // server management tasks are performed using this user account - us.addUser(RegistryConstants.SYSTEM_USER, RegistryConstants.SYSTEM_PASSWORD); - - // Add a user called samples. The sample user is managed by the mashup server and - // all samples are deployed using this account - us.addUser(MashupConstants.SAMPLES_USER, MashupConstants.SAMPLES_USER_PASSWORD); - - // Add an admin role so that admins can be put into this role - us.addRole(RegistryConstants.ADMIN_ROLE); - - // Add an everyone role so that all users are added to this role - us.addRole(RegistryConstants.EVERYONE_ROLE); - - // Assign system user the 'admin' role. - us.addUserToRole(MashupConstants.SYSTEM_USER, RegistryConstants.ADMIN_ROLE); - - AccessControlAdmin ac = realm.getAccessControlAdmin(); - - // Setting permissions for the admin role on user resources - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.USER_RESOURCE, - AccessControlConstants.ADD); - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.USER_RESOURCE, - AccessControlConstants.READ); - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.USER_RESOURCE, - AccessControlConstants.EDIT); - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.USER_RESOURCE, - AccessControlConstants.DELETE); - - // Setting permissions for the admin role on role resources - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.ROLE_RESOURCE, - AccessControlConstants.ADD); - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.ROLE_RESOURCE, - AccessControlConstants.READ); - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.ROLE_RESOURCE, - AccessControlConstants.EDIT); - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, AccessControlConstants.ROLE_RESOURCE, - AccessControlConstants.DELETE); - - // Setting permissions for the admin role on setting permissions - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, - AccessControlConstants.USER_PERMISSION_RESOURCE, - AccessControlConstants.READ); - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, - AccessControlConstants.USER_PERMISSION_RESOURCE, - AccessControlConstants.ADD); - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, - AccessControlConstants.USER_PERMISSION_RESOURCE, - AccessControlConstants.EDIT); - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, - AccessControlConstants.USER_PERMISSION_RESOURCE, - AccessControlConstants.DELETE); - - // Setting permissions for the admin role to GET, PUT and DELETE data from any part - // of the registry - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, - ActionConstants.GET); - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, - ActionConstants.PUT); - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, - ActionConstants.DELETE); - ac.authorizeRole(RegistryConstants.ADMIN_ROLE, RegistryConstants.ROOT_PATH, - AccessControlConstants.AUTHORIZE); - - // Setting permissions for the everyone role to GET data from any part - // of the registry - ac.authorizeRole(RegistryConstants.EVERYONE_ROLE, RegistryConstants.ROOT_PATH, - ActionConstants.GET); - ac.authorizeRole(RegistryConstants.EVERYONE_ROLE, RegistryConstants.ROOT_PATH, - ActionConstants.GET); - - // Setting permissions for the everyone role to READ and EDIT user resources - ac.authorizeRole(RegistryConstants.EVERYONE_ROLE, - AccessControlConstants.USER_RESOURCE, AccessControlConstants.READ); - ac.authorizeRole(RegistryConstants.EVERYONE_ROLE, - AccessControlConstants.USER_RESOURCE, AccessControlConstants.EDIT); - - // Setting permissions for the everyone role to READ permissions - ac.authorizeRole(RegistryConstants.EVERYONE_ROLE, - AccessControlConstants.ROLE_RESOURCE, AccessControlConstants.READ); - ac.authorizeRole(RegistryConstants.EVERYONE_ROLE, - AccessControlConstants.USER_PERMISSION_RESOURCE, - AccessControlConstants.READ); - - // Setting permissions for the everyone role to READ all mashups and queries - ac.authorizeRole(MashupConstants.EVERYONE_ROLE, MashupConstants.ALL_MASHUPS_PATH, - AccessControlConstants.READ); - ac.authorizeRole(MashupConstants.EVERYONE_ROLE, - MashupConstants.ALL_MASHUPS_QUERY_PATH, - AccessControlConstants.READ); - } - - createPrimary = Boolean.parseBoolean( - serverConfig.getFirstProperty(MashupConstants.PRIMARY_USER_CONFIG + "." + - MashupConstants.CREATE_PRIMARY_USER)); - - // If the server.xml contains primary account details and if this is the first startup - // then create the primary account - if (createPrimary && !MashupUtils.isInitialSetupComplete()) { - primaryName = - serverConfig.getFirstProperty(MashupConstants.PRIMARY_USER_CONFIG + "." + - MashupConstants.PRIMARY_USER); - primaryPassword = - serverConfig.getFirstProperty(MashupConstants.PRIMARY_USER_CONFIG + "." + - MashupConstants.PRIMARY_PASSWORD); - us.addUser(primaryName, primaryPassword); - } - - RegistryDataSource datasource = new RegistryDataSource(registryUrl, registryDriverClass, - registryUsername, - registryPassword); - - EmbeddedRegistry embeddedRegistry = new EmbeddedRegistry(datasource, realm); - - // Add the realm and registry as parameters in the AxisConfiguration so that they can be - // used later on as an when needed - configCtx.getAxisConfiguration() - .addParameter(RegistryConstants.REGISTRY, embeddedRegistry); - configCtx.getAxisConfiguration().addParameter(RegistryConstants.REGISTRY_REALM, realm); - - // Instantiating a secure registry using the system account, in order to create user - // profiles. - systemRegistry = embeddedRegistry.getSystemRegistry(); - - if (!systemRegistry.resourceExists(MashupConstants.QUERYSTORE_QUERY_PATH)) { - - // Create an area in the registry to add queries - Collection queryCollection = systemRegistry.newCollection(); - systemRegistry.put(MashupConstants.QUERYSTORE_QUERY_PATH, queryCollection); - - // All mashups query - addQuery( - systemRegistry, - MashupConstants.ALL_MASHUPS_QUERY_PATH, - "SELECT PATH FROM RESOURCE WHERE (MEDIA_TYPE='" + - MashupConstants.MASHUP_MEDIA_TYPE + "' OR " + - "MEDIA_TYPE='wso2_data_service') ORDER BY LAST_UPDATED_TIME DESC", - RegistryConstants.RESOURCES_RESULT_TYPE - ); - - // Mashups from a specific user - addQuery( - systemRegistry, - MashupConstants.MY_MASHUPS_QUERY_PATH, - "SELECT PATH FROM RESOURCE WHERE CREATOR=? AND " + - "(MEDIA_TYPE='" + MashupConstants.MASHUP_MEDIA_TYPE + "' OR " + - "MEDIA_TYPE='wso2_data_service') ORDER BY LAST_UPDATED_TIME DESC", - RegistryConstants.RESOURCES_RESULT_TYPE - ); - - // Mashups rated 4.0 or better, sorted by rating. - addQuery( - systemRegistry, - MashupConstants.TOP_MASHUPS_QUERY_PATH, - "SELECT A.PATH FROM RESOURCE A, RATING R WHERE " + - "A.AID=R.AID AND (A.MEDIA_TYPE='" + - MashupConstants.MASHUP_MEDIA_TYPE + "' OR " + - "A.MEDIA_TYPE='wso2_data_service') GROUP BY A.PATH HAVING " + - "AVG(R.RATING) >= 4.0 ORDER BY AVG(R.RATING) DESC", - RegistryConstants.RESOURCES_RESULT_TYPE - ); - - // Mashups from a specific user rated 4.0 or better, sorted by rating. - addQuery( - systemRegistry, - MashupConstants.MY_TOP_MASHUPS_QUERY_PATH, - "SELECT A.PATH FROM RESOURCE A, RATING R WHERE " + - "A.AID=R.AID AND A.CREATOR=? AND (A.MEDIA_TYPE='" + - MashupConstants.MASHUP_MEDIA_TYPE + "' OR " + - "A.MEDIA_TYPE='wso2_data_service') GROUP BY A.PATH HAVING " + - "AVG(R.RATING) >= 4.0 ORDER BY AVG(R.RATING) DESC", - RegistryConstants.RESOURCES_RESULT_TYPE - ); - - // Create a collection for users. All user related data will go in here - Collection usersCollection = systemRegistry.newCollection(); - systemRegistry.put(MashupConstants.USERS_PATH, usersCollection); - - // Create a collection for profiles. All user profiles data will go in here - Collection profileCollection = systemRegistry.newCollection(); - systemRegistry.put(MashupConstants.PROFILES_PATH, profileCollection); - - // Create a collection for system. All system related data will go in here - Collection systemCollection = systemRegistry.newCollection(); - systemRegistry.put(MashupConstants.SYSTEM_PATH, systemCollection); - - // Create a collection for /system/queries. All system queries will go in here - Collection queriesCollection = systemRegistry.newCollection(); - systemRegistry.put(MashupConstants.SYSTEM__QUERIES_PATH, queriesCollection); - - // Create a collection for mashups. All mashup related data will go in here - Collection mashupsCollection = systemRegistry.newCollection(); - systemRegistry.put(MashupConstants.ALL_MASHUPS_PATH, mashupsCollection); - - // This query accepts a resource path, a username, and a tag name, and returns the - // resource path if that user applied that tag to the resource. - // Used for checking whether the current user should be allowed to delete the tag. - addQuery( - systemRegistry, - MashupConstants.USER_TAGS_QUERY_PATH, - "SELECT A.PATH FROM RESOURCE A, TAG T WHERE A.PATH=? " + - "AND T.TAG_NAME=? AND T.RID=A.RID AND T.USER_ID=? AND " + - "(A.MEDIA_TYPE='" + MashupConstants.MASHUP_MEDIA_TYPE + - "' OR A.MEDIA_TYPE='wso2_data_service')", - RegistryConstants.RESOURCES_RESULT_TYPE - ); - - // Query for mashups based on tag, description, or title - // todo: queries aren't case insensitive - addQuery( - systemRegistry, - MashupConstants.CUSTOM_QUERY_PATH, - "SELECT DISTINCT A.PATH FROM RESOURCE A, TAG T WHERE " + - "(A.PATH LIKE ? OR A.DESCRIPTION LIKE ? OR (T.TAG_NAME=? AND " + - "T.RID=A.RID)) AND (A.MEDIA_TYPE='" + MashupConstants - .MASHUP_MEDIA_TYPE + - "' OR A.MEDIA_TYPE='wso2_data_service')", - RegistryConstants.RESOURCES_RESULT_TYPE - ); - - // Query for mashups from a specific user based on tag, description, or title - // todo: queries aren't case insensitive - addQuery( - systemRegistry, - MashupConstants.MY_CUSTOM_QUERY_PATH, - "SELECT DISTINCT A.PATH FROM RESOURCE A, TAG T WHERE " + - "(A.PATH LIKE ? OR A.DESCRIPTION LIKE ? OR (T.TAG_NAME=? AND " + - "T.RID=A.RID)) AND A.CREATOR=? AND (A.MEDIA_TYPE='" + - MashupConstants.MASHUP_MEDIA_TYPE + - "' OR A.MEDIA_TYPE='wso2_data_service')", - RegistryConstants.RESOURCES_RESULT_TYPE - ); - - // Query for comments - addQuery( - systemRegistry, - MashupConstants.COMMENTS_QUERY_PATH, - "SELECT C.CM_ID FROM COMMENT C, RESOURCE A WHERE C.COMMENT_TEXT " + - "LIKE ? AND C.RID=A.RID AND (A.MEDIA_TYPE='" + - MashupConstants.MASHUP_MEDIA_TYPE + - "' OR A.MEDIA_TYPE='wso2_data_service')", - RegistryConstants.COMMENTS_RESULT_TYPE - ); - - // Query for comments by a particular user - addQuery( - systemRegistry, - MashupConstants.MY_COMMENTS_QUERY_PATH, - "SELECT C.CM_ID FROM COMMENT C, RESOURCE A WHERE C.COMMENT_TEXT " + - "LIKE ? AND C.USER_ID=? AND C.RID=A.RID AND " + - "(A.MEDIA_TYPE='" + MashupConstants.MASHUP_MEDIA_TYPE + - "' OR A.MEDIA_TYPE='wso2_data_service')", - RegistryConstants.COMMENTS_RESULT_TYPE - ); - - // Query for all comments by a particular user - addQuery( - systemRegistry, - MashupConstants.USERS_COMMENTS_QUERY_PATH, - "SELECT C.CM_ID FROM COMMENT C, RESOURCE A WHERE C.USER_ID=? AND " + - "C.RID=A.RID AND AND (A.MEDIA_TYPE='" + - MashupConstants.MASHUP_MEDIA_TYPE + - "' OR A.MEDIA_TYPE='wso2_data_service')", - RegistryConstants.COMMENTS_RESULT_TYPE - ); - - // Create the anonymous user profile. - RegistryUtils.createUser(systemRegistry, - RegistryConstants.ANONYMOUS_USER, ANNONYMOUS_USER_NAME, NONE, - ANNONYMOUS_USER_BIO); - - // Create the system user profile. - RegistryUtils.createUser(systemRegistry, MashupConstants.SYSTEM_USER, - SYSTEM_USER_NAME, NONE, SYSTEM_USER_BIO); - - // Create the samples user and profile. - RegistryUtils.createUser(systemRegistry, MashupConstants.SAMPLES_USER, - SAMPLES_USER_NAME, NONE, SAMPLES_USER_BIO); - } - - // If primary user has not been created, do so. - if (createPrimary && !MashupUtils.isInitialSetupComplete()) { - //Create the admin profile using information from a config file. - RegistryUtils - .createUser(systemRegistry, primaryName, primaryName, NONE, - ADMIN_USER_BIO); - // Assign system user the 'admin' role and make primary. - us.addUserToRole(primaryName, RegistryConstants.ADMIN_ROLE); - RegistryUtils.makeUserPrimary(realm, primaryName); - MashupUtils.setInitialSetupComplete(true); - } - } catch (UserStoreException e) { - throw new MashupFault(e); - } catch (RegistryException e) { - throw new MashupFault(e); - } catch (AxisFault axisFault) { - throw new MashupFault(axisFault); - } - } - - private static void addQuery(Registry registry, String path, String sql, String type) - throws RegistryException { - ResourceImpl q = new ResourceImpl(); - q.setContent(sql); - q.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE); - q.setProperty(RegistryConstants.RESULT_TYPE_PROPERTY_NAME, type); - q.setPath(path); - registry.put(path, q); - } } \ No newline at end of file _______________________________________________ Mashup-dev mailing list [email protected] http://www.wso2.org/cgi-bin/mailman/listinfo/mashup-dev
