001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.collab.channel.filesharing.projecthandler;
042:
043: import com.sun.collablet.CollabException;
044:
045: import org.openide.filesystems.FileObject;
046:
047: import java.util.*;
048:
049: import org.netbeans.api.project.FileOwnerQuery;
050: import org.netbeans.api.project.Project;
051: import org.netbeans.api.project.ProjectUtils;
052:
053: import org.netbeans.modules.collab.channel.filesharing.FilesharingConstants;
054: import org.netbeans.modules.collab.channel.filesharing.FilesharingContext;
055: import org.netbeans.modules.collab.channel.filesharing.filesystem.CollabFilesystem;
056: import org.netbeans.modules.collab.core.Debug;
057:
058: /**
059: * SharedProjectManager
060: *
061: * @author ayub.khan@sun.com
062: * @version 1.0
063: */
064: public class SharedProjectManager extends Object implements
065: FilesharingConstants {
066: ////////////////////////////////////////////////////////////////////////////
067: // Instance variables
068: ////////////////////////////////////////////////////////////////////////////
069: private boolean isValid = true;
070:
071: /* context */
072: private FilesharingContext context = null;
073:
074: /*CollabFilesystem*/
075: private CollabFilesystem fs = null;
076:
077: /* all shared projects in this conversation */
078: private HashMap sharedProjects = new HashMap();
079:
080: /**
081: *
082: * @param context
083: */
084: public SharedProjectManager(FilesharingContext context) {
085: super ();
086: this .context = context;
087: }
088:
089: ////////////////////////////////////////////////////////////////////////////
090: // methods
091: ////////////////////////////////////////////////////////////////////////////
092:
093: /**
094: * getContext
095: *
096: * @return context
097: */
098: public FilesharingContext getContext() {
099: return this .context;
100: }
101:
102: /**
103: * getCollabFilesystem
104: *
105: * @return fs
106: */
107: public CollabFilesystem getCollabFilesystem() {
108: if (fs == null) {
109: fs = (CollabFilesystem) context.getCollabFilesystem();
110: }
111:
112: return this .fs;
113: }
114:
115: public void addSharedProject(String userID, String projectName,
116: SharedProject sharedProject) {
117: synchronized (sharedProjects) {
118: sharedProjects.put(userID + "_" + projectName,
119: sharedProject);
120: }
121: }
122:
123: /**
124: * isShared
125: *
126: * @param projectName
127: */
128: public boolean isShared(String userID, String projectName) {
129: synchronized (sharedProjects) {
130: return sharedProjects.containsKey(userID + "_"
131: + projectName);
132: }
133: }
134:
135: /**
136: * getProject
137: *
138: * @param project
139: */
140: public Project getProject(FileObject fo) {
141: return FileOwnerQuery.getOwner(fo);
142: }
143:
144: /**
145: * isSharedOriginal
146: *
147: * @param project
148: */
149: public boolean isSharedOriginal(Project project) {
150: Debug
151: .log(
152: "ProjectsRootNode",
153: (("ProjectsRootNode, check project: " + project) != null) ? ProjectUtils
154: .getInformation(project).getName()
155: : "null");
156:
157: SharedProject[] sharedProjects = getAllSharedProjects();
158:
159: for (int i = 0; i < sharedProjects.length; i++) {
160: Debug.log("ProjectsRootNode",
161: "ProjectsRootNode, sharedProjects: "
162: + sharedProjects.length);
163:
164: Project originalProject = sharedProjects[i]
165: .getOriginalProject();
166:
167: //check if original project already shared
168: if (project == originalProject) {
169: return true;
170: }
171: }
172:
173: return false;
174: }
175:
176: public SharedProject getSharedProject(String userID,
177: String projectName) {
178: synchronized (sharedProjects) {
179: return (SharedProject) sharedProjects.get(userID + "_"
180: + projectName);
181: }
182: }
183:
184: public SharedProject[] getOwnerSharedProjects(String userID) {
185: if ((userID == null) || userID.equals("")) {
186: return null;
187: }
188:
189: List spList = new ArrayList();
190: SharedProject[] sps = getAllSharedProjects();
191:
192: for (int i = 0; i < sps.length; i++) {
193: if (userID.equals(sps[i].getProjectOwner())) {
194: spList.add(sps[i]);
195: }
196: }
197:
198: return (SharedProject[]) spList.toArray(new SharedProject[0]);
199: }
200:
201: public SharedProject[] getAllSharedProjects() {
202: synchronized (sharedProjects) {
203: return (SharedProject[]) sharedProjects.values().toArray(
204: new SharedProject[0]);
205: }
206: }
207:
208: public void removeSharedProject(String userID, String projectName) {
209: synchronized (sharedProjects) {
210: sharedProjects.remove(userID + "_" + projectName);
211: }
212: }
213:
214: /**
215: * setValid
216: *
217: * @param status
218: * @throws CollabException
219: */
220: public void setValid(boolean valid) throws CollabException {
221: this .isValid = valid;
222: }
223:
224: /**
225: * getValid
226: *
227: * @return status if false handler is invalid
228: * @throws CollabException
229: */
230: public boolean isValid() throws CollabException {
231: return this .isValid;
232: }
233:
234: /**
235: * print
236: *
237: */
238: public void clear() {
239: SharedProject[] sps = getAllSharedProjects();
240:
241: for (int i = 0; i < sps.length; i++) {
242: try {
243: sps[i].finalize();
244: } catch (Throwable th) {
245: th.printStackTrace(Debug.out);
246: }
247: }
248:
249: synchronized (sharedProjects) {
250: sharedProjects.clear();
251: }
252: }
253:
254: /**
255: * print
256: *
257: */
258: public void print() {
259: if (!Debug.isAllowed("FilesharingContext")) {
260: return;
261: }
262:
263: synchronized (sharedProjects) {
264: Debug.log("FilesharingContext", "sharedProjects: " + //NoI18n
265: sharedProjects.keySet().toString()); //NoI18n
266: }
267: }
268: }
|