Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 515770 - Multiple workspace support
  • Loading branch information
squarti committed Apr 27, 2017
1 parent e62d7de commit 4dee497
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 88 deletions.
Expand Up @@ -130,21 +130,15 @@ public void createProject(ProjectInfo projectInfo) throws CoreException {
if (projectInfo.getFullName() == null) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1, "SimpleMetaStore.createProject: project name is null.", null));
}
WorkspaceInfo workspaceInfo;
try {
workspaceInfo = readWorkspace(projectInfo.getWorkspaceId());
} catch (CoreException exception) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1,
"SimpleMetaStore.createProject: could not find workspace with id:" + projectInfo.getWorkspaceId() + ", workspace does not exist.", null));
}
if (workspaceInfo == null) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1,
"SimpleMetaStore.createProject: could not find workspace with id:" + projectInfo.getWorkspaceId() + ", workspace does not exist.", null));
}
String userId = SimpleMetaStoreUtil.decodeUserIdFromWorkspaceId(projectInfo.getWorkspaceId());
ReadWriteLock lock = getLockForUser(userId);
lock.writeLock().lock();
try {
WorkspaceInfo workspaceInfo = readWorkspace(projectInfo.getWorkspaceId());
if (workspaceInfo == null) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1,
"SimpleMetaStore.createProject: could not find workspace with id:" + projectInfo.getWorkspaceId() + ", workspace does not exist.", null));
}
String encodedWorkspaceName = SimpleMetaStoreUtil.decodeWorkspaceNameFromWorkspaceId(projectInfo.getWorkspaceId());
String projectId = SimpleMetaStoreUtil.encodeProjectIdFromProjectName(projectInfo.getFullName());
projectInfo.setUniqueId(projectId);
Expand Down Expand Up @@ -176,7 +170,11 @@ public void createProject(ProjectInfo projectInfo) throws CoreException {
"SimpleMetaStore.createProject: could not create project: " + projectInfo.getFullName() + " for user " + userId, null));
}
}
if (!SimpleMetaStoreUtil.createMetaFile(userMetaFolder, projectId, jsonObject)) {
String metafileId = projectId;
if (!DEFAULT_WORKSPACE_NAME.equals(workspaceInfo.getFullName())) {
metafileId = workspaceInfo.getUniqueId() + SimpleMetaStoreUtil.SEPARATOR + projectId;
}
if (!SimpleMetaStoreUtil.createMetaFile(userMetaFolder, metafileId, jsonObject)) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1,
"SimpleMetaStore.createProject: could not create project: " + projectInfo.getFullName() + " for user " + userId, null));
}
Expand All @@ -187,6 +185,9 @@ public void createProject(ProjectInfo projectInfo) throws CoreException {
newProjectNames.add(projectInfo.getFullName());
workspaceInfo.setProjectNames(newProjectNames);
updateWorkspace(workspaceInfo);
} catch (CoreException exception) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1,
"SimpleMetaStore.createProject: could not find workspace with id:" + projectInfo.getWorkspaceId() + ", workspace does not exist.", null));
} finally {
lock.writeLock().unlock();
}
Expand Down Expand Up @@ -257,12 +258,6 @@ public void createWorkspace(WorkspaceInfo workspaceInfo) throws CoreException {
throw new CoreException(
new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1, "SimpleMetaStore.createWorkspace: workspace name is null.", null));
}
if (!SimpleMetaStore.DEFAULT_WORKSPACE_NAME.equals(workspaceInfo.getFullName())) {
// The workspace name you create must be Orion Content. See Bug 439735
logger.info("SimpleMetaStore.createWorkspace: workspace name conflict: name will be \"Orion Content\": user " + workspaceInfo.getUserId()
+ " provided " + workspaceInfo.getFullName() + " instead.");
workspaceInfo.setFullName(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
}
UserInfo userInfo;
try {
userInfo = readUser(workspaceInfo.getUserId());
Expand All @@ -274,20 +269,12 @@ public void createWorkspace(WorkspaceInfo workspaceInfo) throws CoreException {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1,
"SimpleMetaStore.createWorkspace: could not find user with id: " + workspaceInfo.getUserId() + ", user does not exist.", null));
}
if (!userInfo.getWorkspaceIds().isEmpty()) {
// We have an existing workspace already, you cannot create a second workspace. See Bug 439735
String existingWorkspaceIds = userInfo.getWorkspaceIds().get(0);
logger.info("SimpleMetaStore.createWorkspace: workspace conflict: cannot create a second workspace for user id: " + userInfo.getUniqueId()
+ ", existing workspace is being used: " + existingWorkspaceIds);
workspaceInfo.setUniqueId(existingWorkspaceIds);
return;
}
// lock the user when creating the workspace and updating the user.
ReadWriteLock lock = getLockForUser(userInfo.getUniqueId());
lock.writeLock().lock();
try {
// We create a meta folder for the workspace using the encoded workspace name
String workspaceId = SimpleMetaStoreUtil.encodeWorkspaceId(workspaceInfo.getUserId(), workspaceInfo.getFullName());
String workspaceId = SimpleMetaStoreUtil.encodeWorkspaceId(workspaceInfo.getUserId(), workspaceInfo.getUniqueId() != null ? workspaceInfo.getUniqueId() : workspaceInfo.getFullName());
String encodedWorkspaceName = SimpleMetaStoreUtil.decodeWorkspaceNameFromWorkspaceId(workspaceId);
// It is possible to have two workspaces with the same name, so append an integer if this is a duplicate
// name.
Expand Down Expand Up @@ -358,7 +345,11 @@ public void deleteProject(String workspaceId, String projectName) throws CoreExc
updateWorkspace(workspaceInfo);

String projectId = SimpleMetaStoreUtil.encodeProjectIdFromProjectName(projectName);
if (!SimpleMetaStoreUtil.deleteMetaFile(userMetaFolder, projectId)) {
String metafileId = projectId;
if (!SimpleMetaStoreUtil.encodeWorkspaceId(userId, DEFAULT_WORKSPACE_NAME).equals(workspaceId)) {
metafileId = workspaceId + SimpleMetaStoreUtil.SEPARATOR + metafileId;
}
if (!SimpleMetaStoreUtil.deleteMetaFile(userMetaFolder, metafileId)) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1,
"SimpleMetaStore.deleteProject: could not delete project: " + projectName + " for user " + userId, null));
}
Expand Down Expand Up @@ -441,9 +432,11 @@ public void deleteWorkspace(String userId, String workspaceId) throws CoreExcept
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1,
"SimpleMetaStore.deleteWorkspace: could not delete workspace: " + encodedWorkspaceName, null));
}
if (!SimpleMetaStoreUtil.deleteMetaFolder(userMetaFolder, encodedWorkspaceName, true)) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1,
"SimpleMetaStore.deleteWorkspace: could not delete workspace: " + encodedWorkspaceName, null));
if (SimpleMetaStoreUtil.isMetaFolder(userMetaFolder, encodedWorkspaceName)) {
if (!SimpleMetaStoreUtil.deleteMetaFolder(userMetaFolder, encodedWorkspaceName, true)) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1,
"SimpleMetaStore.deleteWorkspace: could not delete workspace: " + encodedWorkspaceName, null));
}
}
} finally {
lock.writeLock().unlock();
Expand Down Expand Up @@ -636,10 +629,14 @@ public ProjectInfo readProject(String workspaceId, String projectName) throws Co
return null;
}
String projectId = SimpleMetaStoreUtil.encodeProjectIdFromProjectName(projectName);
JSONObject jsonObject = SimpleMetaStoreUtil.readMetaFile(userMetaFolder, projectId);
String metafileId = projectId;
if (!SimpleMetaStoreUtil.encodeWorkspaceId(userId, DEFAULT_WORKSPACE_NAME).equals(workspaceId)) {
metafileId = workspaceId + SimpleMetaStoreUtil.SEPARATOR + metafileId;
}
JSONObject jsonObject = SimpleMetaStoreUtil.readMetaFile(userMetaFolder, metafileId);
ProjectInfo projectInfo = new ProjectInfo();
if (jsonObject == null) {
if (SimpleMetaStoreUtil.isMetaFolder(workspaceMetaFolder, projectId) && !SimpleMetaStoreUtil.isMetaFile(userMetaFolder, projectId)) {
if (SimpleMetaStoreUtil.isMetaFolder(workspaceMetaFolder, projectId) && !SimpleMetaStoreUtil.isMetaFile(userMetaFolder, metafileId)) {
// the project folder exists but the project json file does not, so create it
File projectMetaFolder = SimpleMetaStoreUtil.readMetaFolder(workspaceMetaFolder, projectId);
if (logger.isDebugEnabled()) {
Expand All @@ -653,7 +650,7 @@ public ProjectInfo readProject(String workspaceId, String projectName) throws Co
lock.readLock().unlock();
createProject(projectInfo);
lock.readLock().lock();
jsonObject = SimpleMetaStoreUtil.readMetaFile(userMetaFolder, projectId);
jsonObject = SimpleMetaStoreUtil.readMetaFile(userMetaFolder, metafileId);
} else {
// both the project folder and project json do not exist, no project
// OR both the project folder and project json exist, but bad project JSON file == no project
Expand Down Expand Up @@ -792,9 +789,6 @@ public WorkspaceInfo readWorkspace(String workspaceId) throws CoreException {
return null;
}
File userMetaFolder = SimpleMetaStoreUtil.readMetaUserFolder(getRootLocation(), userId);
if (!SimpleMetaStoreUtil.isMetaFolder(userMetaFolder, encodedWorkspaceName)) {
return null;
}
JSONObject jsonObject = null;
// add a read lock when reading the the.
ReadWriteLock lock = getLockForUser(userId);
Expand Down Expand Up @@ -910,18 +904,26 @@ public void updateProject(ProjectInfo projectInfo) throws CoreException {
String encodedWorkspaceName = SimpleMetaStoreUtil.decodeWorkspaceNameFromWorkspaceId(projectInfo.getWorkspaceId());
File userMetaFolder = SimpleMetaStoreUtil.readMetaUserFolder(getRootLocation(), userId);
File workspaceMetaFolder = SimpleMetaStoreUtil.readMetaFolder(userMetaFolder, encodedWorkspaceName);
if (!SimpleMetaStoreUtil.isMetaFile(userMetaFolder, projectInfo.getUniqueId())) {
String metafileId = projectInfo.getUniqueId();
if (!SimpleMetaStoreUtil.encodeWorkspaceId(userId, DEFAULT_WORKSPACE_NAME).equals(projectInfo.getWorkspaceId())) {
metafileId = projectInfo.getWorkspaceId() + SimpleMetaStoreUtil.SEPARATOR + metafileId;
}
if (!SimpleMetaStoreUtil.isMetaFile(userMetaFolder, metafileId)) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1,
"SimpleMetaStore.updateProject: could not update project: " + projectInfo.getFullName() + " for workspace " + encodedWorkspaceName,
null));
}
JSONObject jsonObject = SimpleMetaStoreUtil.readMetaFile(userMetaFolder, projectInfo.getUniqueId());
JSONObject jsonObject = SimpleMetaStoreUtil.readMetaFile(userMetaFolder, metafileId);
if (!projectInfo.getUniqueId().equals(SimpleMetaStoreUtil.encodeProjectIdFromProjectName(projectInfo.getFullName()))) {
IFileStore projectStore = projectInfo.getProjectStore();
IFileStore defaultProjectStore = getDefaultContentLocation(readProject(projectInfo.getWorkspaceId(), projectInfo.getUniqueId()));
// full name has changed, this is a project move
String newProjectId = SimpleMetaStoreUtil.encodeProjectIdFromProjectName(projectInfo.getFullName());
if (!SimpleMetaStoreUtil.moveMetaFile(userMetaFolder, projectInfo.getUniqueId(), newProjectId)) {
String newMetafileId = newProjectId;
if (!SimpleMetaStoreUtil.encodeWorkspaceId(userId, DEFAULT_WORKSPACE_NAME).equals(projectInfo.getWorkspaceId())) {
newMetafileId = projectInfo.getWorkspaceId() + SimpleMetaStoreUtil.SEPARATOR + newMetafileId;
}
if (!SimpleMetaStoreUtil.moveMetaFile(userMetaFolder, metafileId, newMetafileId)) {
throw new CoreException(
new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1, "SimpleMetaStore.updateProject: could not move project: "
+ projectInfo.getUniqueId() + " to " + projectInfo.getFullName() + " for workspace " + encodedWorkspaceName, null));
Expand Down Expand Up @@ -1002,7 +1004,11 @@ public void updateProject(ProjectInfo projectInfo) throws CoreException {
"SimpleMetaStore.updateProject: could not update project: " + projectInfo.getFullName() + " for workspace " + encodedWorkspaceName,
null));
}
if (!SimpleMetaStoreUtil.updateMetaFile(userMetaFolder, projectInfo.getUniqueId(), jsonObject)) {
metafileId = projectInfo.getUniqueId();
if (!SimpleMetaStoreUtil.encodeWorkspaceId(userId, DEFAULT_WORKSPACE_NAME).equals(projectInfo.getWorkspaceId())) {
metafileId = projectInfo.getWorkspaceId() + SimpleMetaStoreUtil.SEPARATOR + metafileId;
}
if (!SimpleMetaStoreUtil.updateMetaFile(userMetaFolder, metafileId, jsonObject)) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1,
"SimpleMetaStore.updateProject: could not update project: " + projectInfo.getFullName() + " for workspace " + encodedWorkspaceName,
null));
Expand Down
Expand Up @@ -424,7 +424,7 @@ public static String encodeProjectIdFromProjectName(String projectName) {
* @return The workspace id.
*/
public static String encodeWorkspaceId(String userName, String workspaceName) {
String workspaceId = workspaceName.replace(" ", "").replace("#", "");
String workspaceId = workspaceName.replace(" ", "").replace("#", "").replaceAll("-", "");
return userName + SEPARATOR + workspaceId;
}

Expand Down
Expand Up @@ -184,8 +184,14 @@ private static char[] getPassword() {
String password = System.getProperty(ORION_STORAGE_PASSWORD, "unspecified"); //$NON-NLS-1$
return password.toCharArray();
}

public static void main(String[] args) {
System.out.println(encryptPassword(args[0]));
if (args.length > 2 && "-encode".equals(args[0])) {
System.out.println(encryptPassword(args[1]));
} else if (args.length > 2 && "-decode".equals(args[0])) {
System.out.println(decryptPassword(args[1]));
} else {
System.out.println("Usage: java SimpleUserPasswordUtil <-encode || -decode> value");
}
}
}
Expand Up @@ -252,6 +252,12 @@ public class ProtocolConstants {
*/
public static final String KEY_LOCATION = "Location"; //$NON-NLS-1$

/**
* JSON representation key for the workspace location of an object.
* The value's data type is a String.
*/
public static final String KEY_WORKSPACE_LOCATION = "WorkspaceLocation"; //$NON-NLS-1$

/**
* JSON representation key for an object's name. The value's data type is a String.
*/
Expand Down
Expand Up @@ -72,7 +72,7 @@ public void addAtributesFor(HttpServletRequest request, URI resource, JSONObject
boolean isWorkspace = ("/workspace".equals(servlet)); //$NON-NLS-1$

try {
if (isWorkspace && Method.POST.equals(Method.fromString(request.getMethod()))) {
if (isWorkspace && Method.POST.equals(Method.fromString(request.getMethod())) && representation.has(ProtocolConstants.KEY_CONTENT_LOCATION)) {
String contentLocation = representation.getString(ProtocolConstants.KEY_CONTENT_LOCATION);

// initialize a new git repository on project creation if specified by configuration
Expand Down
Expand Up @@ -102,6 +102,10 @@ public boolean handleRequest(HttpServletRequest request, HttpServletResponse res
}
IPath gitSearchPath = filePath.hasTrailingSeparator() ? filePath : filePath.removeLastSegments(1);
Set<Entry<IPath, File>> gitDirsFound = GitUtils.getGitDirs(gitSearchPath, Traverse.GO_UP).entrySet();
if (gitDirsFound.size() == 0) {
String msg = NLS.bind("Could not find repository for {0}", filePath);
return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
}
Entry<IPath, File> firstGitDir = gitDirsFound.iterator().next();
File gitDir = firstGitDir.getValue();
if (gitDir == null) {
Expand Down
Expand Up @@ -80,10 +80,10 @@ public class GitHandlerV1 extends ServletResourceHandler<String> {
public boolean handleRequest(HttpServletRequest request, HttpServletResponse response, String gitPathInfo) throws ServletException {

String[] infoParts = gitPathInfo.split("\\/", 3); //$NON-NLS-1$
if (infoParts.length < 3)
if (infoParts.length < 2)
return false; // malformed request, we don't know how to handle this

String pathString = infoParts[2];
String pathString = infoParts.length > 2 ? infoParts[2] : "";
if (request.getContextPath().length() != 0) {
IPath path = pathString == null ? Path.EMPTY : new Path(pathString);
IPath contextPath = new Path(request.getContextPath());
Expand Down

0 comments on commit 4dee497

Please sign in to comment.