Source Code Cross Referenced for ModelImpl.java in  » IDE-Netbeans » cnd » org » netbeans » modules » cnd » modelimpl » csm » core » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » cnd » org.netbeans.modules.cnd.modelimpl.csm.core 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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-2007 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:
042:        package org.netbeans.modules.cnd.modelimpl.csm.core;
043:
044:        import java.io.File;
045:        import java.io.IOException;
046:        import org.netbeans.modules.cnd.api.model.*;
047:        import org.netbeans.modules.cnd.apt.utils.APTIncludeUtils;
048:
049:        import java.util.*;
050:        import javax.swing.SwingUtilities;
051:        import org.netbeans.api.project.Project;
052:        import org.netbeans.api.project.ui.OpenProjects;
053:        import org.netbeans.modules.cnd.api.project.NativeProject;
054:        import org.netbeans.modules.cnd.apt.support.APTDriver;
055:        import org.netbeans.modules.cnd.apt.support.APTSystemStorage;
056:        import org.netbeans.modules.cnd.utils.cache.FilePathCache;
057:        import org.netbeans.modules.cnd.utils.cache.TextCache;
058:        import org.netbeans.modules.cnd.modelimpl.cache.CacheManager;
059:        import org.netbeans.modules.cnd.modelimpl.debug.DiagnosticExceptoins;
060:        import org.netbeans.modules.cnd.modelimpl.debug.TraceFlags;
061:        import org.netbeans.modules.cnd.modelimpl.memory.LowMemoryEvent;
062:        import org.netbeans.modules.cnd.modelimpl.memory.LowMemoryListener;
063:        import org.netbeans.modules.cnd.modelimpl.memory.LowMemoryNotifier;
064:        import org.netbeans.modules.cnd.modelimpl.options.CodeAssistanceOptions;
065:        import org.netbeans.modules.cnd.modelimpl.platform.ModelSupport;
066:        import org.netbeans.modules.cnd.modelimpl.repository.RepositoryUtils;
067:        import org.netbeans.modules.cnd.modelimpl.textcache.FileNameCache;
068:        import org.netbeans.modules.cnd.modelimpl.textcache.NameCache;
069:        import org.netbeans.modules.cnd.modelimpl.textcache.ProjectNameCache;
070:        import org.netbeans.modules.cnd.modelimpl.textcache.QualifiedNameCache;
071:        import org.netbeans.modules.cnd.modelimpl.textcache.UniqueNameCache;
072:        import org.netbeans.modules.cnd.modelimpl.uid.UIDCsmConverter;
073:        import org.netbeans.modules.cnd.modelimpl.uid.UIDManager;
074:        import org.openide.util.Cancellable;
075:        import org.openide.util.RequestProcessor;
076:
077:        /**
078:         * CsmModel implementation
079:         * @author Vladimir Kvashin
080:         */
081:        public class ModelImpl implements  CsmModel, LowMemoryListener {
082:
083:            public ModelImpl() {
084:                startup();
085:            }
086:
087:            public static boolean isStandalone() {
088:                return !ModelImpl.class.getClassLoader().getClass().getName()
089:                        .startsWith("org.netbeans."); // NOI18N
090:            }
091:
092:            private void initThreasholds() {
093:                String value, propertyName;
094:                propertyName = "cnd.model.memory.warning.threashold"; // NOI18N
095:                value = System.getProperty(propertyName);
096:                if (value != null) {
097:                    try {
098:                        warningThreshold = Double.parseDouble(value);
099:                    } catch (NumberFormatException e) {
100:                        Utils.LOG.severe("Incorrect format for property "
101:                                + propertyName + ": " + value); // NOI18N
102:                        DiagnosticExceptoins.register(e);
103:                    }
104:                }
105:                //	propertyName = "cnd.model.memory.fatal.threashold";
106:                //	value = System.getProperty(propertyName);
107:                //	if( value != null ) {
108:                //	    try {
109:                //		fatalThreshold = Double.parseDouble(value);
110:                //	    }
111:                //	    catch(NumberFormatException e) {
112:                //		Utils.LOG.severe("Incorrect format for property " + propertyName + ": " + value);
113:                //	    }
114:                //	}
115:            }
116:
117:            public CsmProject findProject(Object id) {
118:                ProjectBase prj = null;
119:                if (id != null) {
120:                    synchronized (lock) {
121:                        prj = obj2Project(id);
122:                    }
123:                }
124:                return prj;
125:            }
126:
127:            private ProjectBase obj2Project(Object obj) {
128:                CsmUID<CsmProject> prjUID = platf2csm.get(obj);
129:                ProjectBase prj = (ProjectBase) UIDCsmConverter
130:                        .UIDtoProject(prjUID);
131:                assert prj != null || prjUID == null : "null object for UID "
132:                        + prjUID;
133:                return prj;
134:            }
135:
136:            public CsmProject getProject(Object id) {
137:                if (id instanceof  Project) {
138:                    NativeProject prj = ((Project) id).getLookup().lookup(
139:                            NativeProject.class);
140:                    if (prj != null) {
141:                        id = prj;
142:                    }
143:                }
144:                return findProject(id);
145:            }
146:
147:            public CsmProject _getProject(Object id) {
148:                ProjectBase prj = null;
149:                if (id != null) {
150:                    synchronized (lock) {
151:                        prj = obj2Project(id);
152:                        if (prj == null && id instanceof  Project) {
153:                            // for compatibility 
154:                            if (TraceFlags.DEBUG) {
155:                                System.err
156:                                        .println("getProject called with Project... expected NativeProject");
157:                                new Throwable().printStackTrace(System.err);
158:                            }
159:                            id = ((Project) id).getLookup().lookup(
160:                                    NativeProject.class);
161:                            prj = id != null ? obj2Project(id) : null;
162:                        }
163:                        if (prj == null) {
164:                            if (disabledProjects.contains(id)) {
165:                                return null;
166:                            }
167:                            String name;
168:                            if (id instanceof  NativeProject) {
169:                                name = ((NativeProject) id)
170:                                        .getProjectDisplayName();
171:                            } else {
172:                                new IllegalStateException(
173:                                        "CsmProject does not exist: " + id)
174:                                        .printStackTrace(System.err); // NOI18N
175:                                name = "<unnamed>"; // NOI18N
176:                            }
177:                            prj = ProjectImpl.createInstance(this ,
178:                                    (NativeProject) id, name);
179:                            putProject2Map(id, prj);
180:                        }
181:                    }
182:                }
183:                return prj;
184:            }
185:
186:            public ProjectBase addProject(NativeProject id, String name,
187:                    boolean enableModel) {
188:                ProjectBase prj = null;
189:                assert (id != null) : "The platform project is null"; // NOI18N
190:                if (enableModel) {
191:                    boolean fireOpened = false;
192:                    synchronized (lock) {
193:                        if (state != CsmModelState.ON) {
194:                            return null;
195:                        }
196:                        prj = obj2Project(id);
197:                        if (prj == null) {
198:                            prj = ProjectImpl.createInstance(this , id, name);
199:                            putProject2Map(id, prj);
200:                            fireOpened = true;
201:                        } else {
202:                            String expectedUniqueName = ProjectBase
203:                                    .getUniqueName(id).toString();
204:                            String defactoUniqueName = prj.getUniqueName()
205:                                    .toString();
206:                            if (!defactoUniqueName.equals(expectedUniqueName)) {
207:                                new IllegalStateException(
208:                                        "Existing project unique name differ: "
209:                                                + defactoUniqueName
210:                                                + " - expected "
211:                                                + expectedUniqueName)
212:                                        .printStackTrace(System.err); // NOI18N
213:                            }
214:                        }
215:                    }
216:                    if (fireOpened) {
217:                        ListenersImpl.getImpl().fireProjectOpened(prj);
218:                    }
219:                } else {
220:                    disabledProjects.add(id);
221:                }
222:                return prj;
223:            }
224:
225:            // for testing purposes only
226:            public ProjectBase addProject(ProjectBase prj) {
227:                synchronized (lock) {
228:                    Object id = prj.getPlatformProject();
229:                    if (obj2Project(id) != null) {
230:                        new IllegalStateException("CsmProject already exists: "
231:                                + id).printStackTrace(System.err); // NOI18N
232:                        return null;
233:                    }
234:                    putProject2Map(id, prj);
235:                }
236:                ListenersImpl.getImpl().fireProjectOpened(prj);
237:                return prj;
238:            }
239:
240:            private void putProject2Map(final Object id, final ProjectBase prj) {
241:                CsmUID<CsmProject> uid = UIDCsmConverter.projectToUID(prj);
242:                platf2csm.put(id, uid);
243:            }
244:
245:            public void closeProject(Object platformProject) {
246:                _closeProject(null, platformProject,
247:                        !TraceFlags.PERSISTENT_REPOSITORY);
248:            }
249:
250:            public void closeProject(Object platformProject,
251:                    boolean cleanRepository) {
252:                _closeProject(null, platformProject, cleanRepository);
253:            }
254:
255:            public void closeProjectBase(ProjectBase prj) {
256:                _closeProject(prj, prj.getPlatformProject(),
257:                        !TraceFlags.PERSISTENT_REPOSITORY);
258:            }
259:
260:            private void _closeProject(final ProjectBase csmProject,
261:                    final Object platformProjectKey,
262:                    final boolean cleanRepository) {
263:                if (SwingUtilities.isEventDispatchThread()) {
264:                    _closeProject2_pre(csmProject, platformProjectKey);
265:                    Runnable task = new Runnable() {
266:                        public void run() {
267:                            _closeProject2(csmProject, platformProjectKey,
268:                                    cleanRepository);
269:                        }
270:                    };
271:                    this .enqueueModelTask(task, "Closing Project "); // NOI18N
272:                } else {
273:                    _closeProject2(csmProject, platformProjectKey,
274:                            cleanRepository);
275:                }
276:            }
277:
278:            public void removeProject(Object platformProject) {
279:                _removeProject(null, platformProject);
280:            }
281:
282:            public void removeProjectBase(ProjectBase prj) {
283:                _removeProject(prj, prj.getPlatformProject());
284:            }
285:
286:            private void _removeProject(final ProjectBase csmProject,
287:                    final Object platformProjectKey) {
288:                if (SwingUtilities.isEventDispatchThread()) {
289:                    _closeProject2_pre(csmProject, platformProjectKey);
290:                    Runnable task = new Runnable() {
291:                        public void run() {
292:                            _closeProject2(csmProject, platformProjectKey, true);
293:                        }
294:                    };
295:                    this .enqueueModelTask(task,
296:                            "Closing Project and cleaning the repository"); // NOI18N
297:                } else {
298:                    _closeProject2(csmProject, platformProjectKey, true);
299:                }
300:            }
301:
302:            private void _closeProject2_pre(ProjectBase csmProject,
303:                    Object platformProjectKey) {
304:                ProjectBase prj = (csmProject == null) ? (ProjectBase) getProject(platformProjectKey)
305:                        : csmProject;
306:                if (prj != null) {
307:                    prj.setDisposed();
308:                }
309:            }
310:
311:            private void _closeProject2(ProjectBase csmProject,
312:                    Object platformProjectKey, boolean cleanRepository) {
313:                ProjectBase prj = csmProject;
314:                boolean cleanModel = false;
315:                synchronized (lock) {
316:                    CsmUID<CsmProject> uid = platf2csm
317:                            .remove(platformProjectKey);
318:                    if (uid != null) {
319:                        prj = (prj == null) ? (ProjectBase) UIDCsmConverter
320:                                .UIDtoProject(uid) : prj;
321:                        assert prj != null : "null object for UID " + uid;
322:                    }
323:                    cleanModel = (platf2csm.size() == 0);
324:                }
325:
326:                if (prj != null) {
327:                    disposeProject(prj, cleanRepository);
328:                    if (!prj.isArtificial()) {
329:                        LibraryManager.getInstance().onProjectClose(
330:                                prj.getUID());
331:                    }
332:                }
333:
334:                if (cleanModel) {
335:                    // time to clean caches
336:                    cleanCaches();
337:                }
338:            }
339:
340:            /*package-local*/void disposeProject(final ProjectBase prj) {
341:                disposeProject(prj, !TraceFlags.PERSISTENT_REPOSITORY);
342:            }
343:
344:            /*package-local*/void disposeProject(final ProjectBase prj,
345:                    boolean cleanRepository) {
346:                assert prj != null;
347:                CharSequence name = prj.getName();
348:                if (TraceFlags.TRACE_CLOSE_PROJECT)
349:                    System.err.println("dispose project " + name);
350:                prj.setDisposed();
351:                ListenersImpl.getImpl().fireProjectClosed(prj);
352:                ParserThreadManager.instance().waitEmptyProjectQueue(prj);
353:                prj.dispose(cleanRepository);
354:                if (TraceFlags.TRACE_CLOSE_PROJECT)
355:                    System.err.println("project closed " + name);
356:            }
357:
358:            public Collection<CsmProject> projects() {
359:                Collection<CsmUID<CsmProject>> vals;
360:                synchronized (lock) {
361:                    vals = new ArrayList<CsmUID<CsmProject>>(platf2csm.values());
362:                }
363:                Collection<CsmProject> out = new ArrayList<CsmProject>(vals
364:                        .size());
365:                for (CsmUID<CsmProject> uid : vals) {
366:                    ProjectBase prj = (ProjectBase) UIDCsmConverter
367:                            .UIDtoProject(uid);
368:                    assert prj != null : "null project for UID " + uid;
369:                    out.add(prj);
370:                }
371:                return out;
372:            }
373:
374:            // used only in a couple of functions below
375:            private final String clientTaskPrefix = "Code Model Client Request"; // NOI18N
376:            private static final String modelTaskPrefix = "Code Model Request Processor"; // NOI18N
377:
378:            public void enqueue(Runnable task, CharSequence name) {
379:                enqueue(RequestProcessor.getDefault(), task, clientTaskPrefix
380:                        + " :" + name); // NOI18N
381:            }
382:
383:            public void enqueue(Runnable task) {
384:                enqueue(RequestProcessor.getDefault(), task, clientTaskPrefix);
385:            }
386:
387:            public static ModelImpl instance() {
388:                return (ModelImpl) CsmModelAccessor.getModel();
389:            }
390:
391:            public Cancellable enqueueModelTask(Runnable task, String name) {
392:                return enqueue(modelProcessor, task, modelTaskPrefix + ": "
393:                        + name); // NOI18N
394:            }
395:
396:            private Cancellable enqueue(RequestProcessor processor,
397:                    final Runnable task, final String taskName) {
398:                return processor.post(new Runnable() {
399:                    public void run() {
400:                        String oldName = Thread.currentThread().getName();
401:                        Thread.currentThread().setName(taskName); // NOI18N
402:                        try {
403:                            task.run();
404:                        } catch (Throwable thr) {
405:                            DiagnosticExceptoins.register(thr);
406:                        } finally {
407:                            Thread.currentThread().setName(oldName);
408:                        }
409:                    }
410:                });
411:            }
412:
413:            public CsmFile findFile(CharSequence absPath) {
414:                Collection/*<CsmProject>*/projects = projects();
415:                for (Iterator it = projects.iterator(); it.hasNext();) {
416:                    CsmProject curPrj = (CsmProject) it.next();
417:                    if (curPrj instanceof  ProjectBase) {
418:                        ProjectBase ownerPrj = ((ProjectBase) curPrj)
419:                                .findFileProject(absPath);
420:                        if (ownerPrj != null) {
421:                            CsmFile csmFile = ownerPrj.findFile(absPath);
422:                            if (csmFile != null) {
423:                                return csmFile;
424:                            }
425:                        }
426:                    }
427:                }
428:                // try the same with canonical path
429:                String canonical;
430:                try {
431:                    canonical = new File(absPath.toString()).getCanonicalPath();
432:                } catch (IOException ex) {
433:                    canonical = null;
434:                }
435:                if (canonical != null && !canonical.equals(absPath)) {
436:                    return findFile(canonical);
437:                }
438:                return null;
439:            }
440:
441:            public CsmModelState getState() {
442:                return state;
443:            }
444:
445:            public void startup() {
446:
447:                if (TraceFlags.TRACE_MODEL_STATE)
448:                    System.err.println("ModelImpl.startup");
449:
450:                ModelSupport.instance().setModel(this );
451:                nativeProjectListener = new NativeProjectListenerImpl(this );
452:
453:                setState(CsmModelState.ON);
454:
455:                if (TraceFlags.CHECK_MEMORY && warningThreshold > 0) {
456:                    LowMemoryNotifier.instance().addListener(this );
457:                    LowMemoryNotifier.instance().setThresholdPercentage(
458:                            warningThreshold);
459:                }
460:
461:                ParserThreadManager.instance().startup(isStandalone());
462:                RepositoryUtils.startup();
463:                //if( ! isStandalone() ) {
464:                //    for( NativeProject nativeProject : ModelSupport.instance().getNativeProjects() ) {
465:                //    	addProject(nativeProject, nativeProject.getProjectDisplayName());
466:                //    }
467:                //}
468:            }
469:
470:            public void shutdown() {
471:
472:                if (TraceFlags.TRACE_MODEL_STATE)
473:                    System.err.println("ModelImpl.shutdown");
474:                setState(CsmModelState.CLOSING);
475:
476:                unregisterProjectListeners();
477:
478:                ParserThreadManager.instance().shutdown();
479:
480:                if (TraceFlags.CHECK_MEMORY) {
481:                    LowMemoryNotifier.instance().removeListener(this );
482:                }
483:
484:                Collection<CsmProject> prjsColl;
485:                Collection<CsmProject> libs = new HashSet<CsmProject>();
486:
487:                synchronized (lock) {
488:                    prjsColl = projects();
489:                    platf2csm.clear();
490:                }
491:
492:                // clearFileExistenceCache all opened projects, UIDs will be removed in disposeProject
493:                for (Iterator projIter = prjsColl.iterator(); projIter
494:                        .hasNext();) {
495:                    ProjectBase project = (ProjectBase) projIter.next();
496:                    disposeProject(project);
497:                    libs.addAll(project.getLibraries());
498:                }
499:                for (Iterator projIter = libs.iterator(); projIter.hasNext();) {
500:                    disposeProject((ProjectBase) projIter.next());
501:                }
502:
503:                cleanCaches();
504:
505:                setState(CsmModelState.OFF);
506:
507:                RepositoryUtils.shutdown();
508:
509:                ModelSupport.instance().setModel(null);
510:            }
511:
512:            public synchronized void registerProjectListeners(
513:                    ProjectBase csmProjectImpl, Object platformProject) {
514:                NativeProject nativeProject = platformProject instanceof  NativeProject ? (NativeProject) platformProject
515:                        : null;
516:                if (nativeProject != null) {
517:                    // The following code removed. It's a project responsibility to call this method only once.
518:                    //	// TODO: fix the problem of registering the same listener twice
519:                    //	// now just remove then add to prevent double instance
520:                    //	nativeProject.removeProjectItemsListener(projectItemListener);
521:                    nativeProject
522:                            .addProjectItemsListener(nativeProjectListener);
523:                }
524:            }
525:
526:            private void unregisterProjectListeners() {
527:                NativeProject[] nativeProjects = ModelSupport
528:                        .getOpenNativeProjects();
529:                for (int i = 0; i < nativeProjects.length; i++) {
530:                    nativeProjects[i]
531:                            .removeProjectItemsListener(nativeProjectListener);
532:                }
533:            }
534:
535:            public void memoryLow(final LowMemoryEvent event) {
536:
537:                double percentage = ((double) event.getUsedMemory() / (double) event
538:                        .getMaxMemory());
539:
540:                final boolean warning = percentage >= warningThreshold
541:                        && projects().size() > 0;
542:
543:                //	final boolean fatal = percentage >= fatalThreshold && projects().size() > 0;
544:                //	
545:                //	if( fatal ) {
546:                //	    LowMemoryNotifier.instance().removeListener(this);
547:                //	}
548:                //	else {
549:                //	    LowMemoryNotifier.instance().setThresholdPercentage(fatalThreshold);
550:                //	}
551:
552:                Runnable runner = new Runnable() {
553:                    public void run() {
554:                        Thread.currentThread().setName(
555:                                "Code model low memory handler"); // NOI18N
556:                        //		if( fatal ) {
557:                        //		    ParserThreadManager.instance().shutdown();
558:                        //		    ModelSupport.instance().onMemoryLow(event, true);
559:                        //		}
560:                        //		else {
561:                        ModelSupport.instance().onMemoryLow(event, false);
562:                        //		}
563:                    }
564:                };
565:                // I have to use Thread directly here (instead of Request processor)
566:                // for the following reasons:
567:                // 1) I have to return control very fast
568:                // 2) if I use RequestProcessor, I can't be sure the thread will be launched -
569:                // what if we already reached the limit for this RequestProcessor?
570:                new Thread(runner).start();
571:
572:            }
573:
574:            private void setState(CsmModelState newState) {
575:                if (TraceFlags.TRACE_MODEL_STATE)
576:                    System.err.println("  ModelImpl.setState " + state + " -> "
577:                            + newState);
578:                if (newState != state) {
579:                    CsmModelState oldState = state;
580:                    state = newState;
581:                    ListenersImpl.getImpl().fireModelStateChanged(newState,
582:                            oldState);
583:                }
584:            }
585:
586:            public void suspend() {
587:                if (TraceFlags.TRACE_MODEL_STATE)
588:                    System.err.println("ModelImpl.suspend");
589:                setState(CsmModelState.SUSPENDED);
590:                ParserQueue.instance().suspend();
591:            }
592:
593:            public void resume() {
594:                if (TraceFlags.TRACE_MODEL_STATE)
595:                    System.err.println("ModelImpl.resume");
596:                setState(CsmModelState.ON);
597:                ParserQueue.instance().resume();
598:            }
599:
600:            public void disableProject(NativeProject nativeProject) {
601:                if (TraceFlags.TRACE_MODEL_STATE)
602:                    System.err.println("ModelImpl.disableProject "
603:                            + nativeProject.getProjectDisplayName());
604:                synchronized (lock) {
605:                    disabledProjects.add(nativeProject);
606:                }
607:                ProjectBase csmProject = (ProjectBase) findProject(nativeProject);
608:                if (csmProject != null) {
609:                    disableProject2(csmProject);
610:                }
611:            }
612:
613:            public void disableProject(ProjectBase csmProject) {
614:                if (TraceFlags.TRACE_MODEL_STATE)
615:                    System.err
616:                            .println("ModelImpl.disableProject " + csmProject);
617:                if (csmProject != null) {
618:                    synchronized (lock) {
619:                        disabledProjects.add(csmProject.getPlatformProject());
620:                    }
621:                    disableProject2(csmProject);
622:                }
623:            }
624:
625:            private void disableProject2(final ProjectBase csmProject) {
626:                csmProject.setDisposed();
627:                Project project = findProjectByNativeProject(ModelSupport
628:                        .getNativeProject(csmProject.getPlatformProject()));
629:                if (project != null) {
630:                    new CodeAssistanceOptions(project)
631:                            .setCodeAssistanceEnabled(Boolean.FALSE);
632:                }
633:                // that's a caller's responsibility to launch disabling in a separate thread
634:                disableProject3(csmProject);
635:            }
636:
637:            private void disableProject3(ProjectBase csmProject) {
638:                if (TraceFlags.TRACE_MODEL_STATE)
639:                    System.err.println("ModelImpl.disableProject3");
640:                suspend();
641:                try {
642:                    while (ParserQueue.instance().isParsing(csmProject)) {
643:                        try {
644:                            if (TraceFlags.TRACE_MODEL_STATE)
645:                                System.err
646:                                        .println("ModelImpl.disableProject3: waiting for current parse...");
647:                            Thread.sleep(100);
648:                        } catch (InterruptedException e) {
649:                        }
650:                    }
651:
652:                    closeProjectBase(csmProject);
653:                    boolean cleanModel;
654:                    synchronized (lock) {
655:                        cleanModel = platf2csm.isEmpty();
656:                    }
657:                    // repository memory cache isn't cleared here,
658:                    // so it's safe to do this outside of the lock
659:                    if (cleanModel) {
660:                        cleanCaches();
661:                    }
662:                } finally {
663:                    resume();
664:                }
665:            }
666:
667:            //    /**
668:            //     * Checks whether there are only library projects.
669:            //     * If yes, returns the set of remaining library projects.
670:            //     * Otherwise returns null
671:            //     */
672:            //    private Set<LibProjectImpl> getLastLibs() {
673:            //	Collection/*<CsmProjects>*/ projects = projects();
674:            //	Set<LibProjectImpl> lastLibs = new HashSet<LibProjectImpl>(projects.size());
675:            //	for (Iterator it = projects.iterator(); it.hasNext();) {
676:            //	    Object e = it.next();
677:            //	    if( e instanceof LibProjectImpl ) {
678:            //		lastLibs.add((LibProjectImpl) e);
679:            //	    }
680:            //	    else {
681:            //		return null;
682:            //	    }
683:            //	}
684:            //	return lastLibs;
685:            //    }
686:
687:            /** Enables/disables code model for the particular ptoject */
688:            public void enableProject(NativeProject nativeProject) {
689:                if (TraceFlags.TRACE_MODEL_STATE)
690:                    System.err.println("ModelImpl.enableProject "
691:                            + nativeProject.getProjectDisplayName());
692:                synchronized (lock) {
693:                    disabledProjects.remove(nativeProject);
694:                }
695:                Project project = findProjectByNativeProject(nativeProject);
696:                if (project != null) {
697:                    new CodeAssistanceOptions(project)
698:                            .setCodeAssistanceEnabled(Boolean.TRUE);
699:                }
700:                addProject(nativeProject,
701:                        nativeProject.getProjectDisplayName(), Boolean.TRUE);
702:                //ProjectBase csmProject = (ProjectBase) _getProject(nativeProject);
703:                //fireProjectOpened(csmProject);
704:                //new CodeAssistanceOptions(findProjectByNativeProject(nativeProject)).setCodeAssistanceEnabled(Boolean.TRUE);
705:            }
706:
707:            public static Project findProjectByNativeProject(
708:                    NativeProject nativeProjectToSearch) {
709:                Project[] projects = OpenProjects.getDefault()
710:                        .getOpenProjects();
711:                for (int i = 0; i < projects.length; i++) {
712:                    NativeProject nativeProject = projects[i].getLookup()
713:                            .lookup(NativeProject.class);
714:                    if (nativeProject != null
715:                            && nativeProject == nativeProjectToSearch) {
716:                        return projects[i];
717:                    }
718:                }
719:                return null;
720:            }
721:
722:            public boolean isProjectEnabled(NativeProject nativeProject) {
723:                ProjectBase project = (ProjectBase) findProject(nativeProject);
724:                return (project != null) && (!project.isDisposing());
725:            }
726:
727:            private void cleanCaches() {
728:                TextCache.dispose();
729:                FilePathCache.dispose();
730:                QualifiedNameCache.getManager().dispose();
731:                NameCache.getManager().dispose();
732:                UniqueNameCache.getManager().dispose();
733:                FileNameCache.getManager().dispose();
734:                ProjectNameCache.getManager().dispose();
735:                if (TraceFlags.USE_AST_CACHE) {
736:                    CacheManager.getInstance().close();
737:                } else {
738:                    APTDriver.getInstance().close();
739:                }
740:                UIDManager.instance().dispose();
741:                APTIncludeUtils.clearFileExistenceCache();
742:                APTSystemStorage.getDefault().dispose();
743:            }
744:
745:            private Object lock = new Object();
746:
747:            private NativeProjectListenerImpl nativeProjectListener;
748:
749:            /** maps platform project to project */
750:            private Map<Object, CsmUID<CsmProject>> platf2csm = new HashMap<Object, CsmUID<CsmProject>>();
751:
752:            private CsmModelState state;
753:
754:            private double warningThreshold = 0.98;
755:            //private double fatalThreshold = 0.99;
756:
757:            private Set<Object> disabledProjects = new HashSet<Object>();
758:
759:            private RequestProcessor modelProcessor = new RequestProcessor(
760:                    "Code model request processor", 1); // NOI18N
761:        }
w__w__w.j_a__v_a_2___s__.___com___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.