View Javadoc

1   package com.explosion.utilities.process;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   import com.explosion.utilities.process.threads.ProcessThread;
8   
9   /*
10   * =============================================================================
11   * 
12   * Copyright 2004 Stephen Cowx
13   * 
14   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
15   * use this file except in compliance with the License. You may obtain a copy of
16   * the License at
17   * 
18   * http://www.apache.org/licenses/LICENSE-2.0
19   * 
20   * Unless required by applicable law or agreed to in writing, software
21   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
23   * License for the specific language governing permissions and limitations under
24   * the License.
25   * 
26   * =============================================================================
27   */
28  
29  /***
30   * @author Stephen Created on Apr 23, 2004
31   */
32  public class ProcessMonitor
33  {
34    private static List allProcesses = new ArrayList();
35    private static List userProcesses = new ArrayList();
36   
37    /***
38     * Add a process to the list of processes
39     * @param processThread
40     * @param isUserProcess - this parameter allows for a disticntion to be made between processes that the 
41     * user has kicked off and processes that the system has kicked off.  This is essentially for listing user processes
42     * as a separate group so that the user can be shown a pretty list and also so that they can 
43     * be deterred from killing processes that they shouldn't
44     */
45    public static synchronized void registerProcess(ProcessThread processThread, boolean isUserProcess)
46    {
47        update();
48        allProcesses.add(processThread);
49        if (processThread.getProcess() != null && processThread.getProcess().isUserProcess())
50            userProcesses.add(processThread);
51    }
52    
53    /***
54     * Get a list of currently active processes
55     * @return
56     */
57    public static List getAllProcessThreads()
58    {
59        update();
60        return allProcesses;
61    }
62  
63    /***
64     * Get a list of currently active User processes
65     * @return
66     */
67    public synchronized static List getUserProcessThreads()
68    {
69        update();
70        return userProcesses;
71    }
72    
73    /***
74     * Update the list of currently active processes and remove dead ones
75     *
76     */
77    private synchronized static void update()
78    {
79        List keepList1 = new ArrayList();
80        List keepList2 = new ArrayList();
81        for (Iterator it = allProcesses.iterator(); it.hasNext();)
82        {
83            ProcessThread p = (ProcessThread) it.next();
84            if (p.getProcess().getPercentComplete() != 100)
85            {
86                keepList1.add(p);
87                if (p.getProcess().isUserProcess())
88                  keepList2.add(p);
89            }
90        }
91        
92        allProcesses = keepList1;
93        userProcesses = keepList2;
94    }
95  
96  }