001    /*
002        Licensed to the Apache Software Foundation (ASF) under one
003        or more contributor license agreements.  See the NOTICE file
004        distributed with this work for additional information
005        regarding copyright ownership.  The ASF licenses this file
006        to you under the Apache License, Version 2.0 (the
007        "License"); you may not use this file except in compliance
008        with the License.  You may obtain a copy of the License at
009    
010           http://www.apache.org/licenses/LICENSE-2.0
011    
012        Unless required by applicable law or agreed to in writing,
013        software distributed under the License is distributed on an
014        "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015        KIND, either express or implied.  See the License for the
016        specific language governing permissions and limitations
017        under the License.  
018     */
019    package org.apache.wiki.ui.progress;
020    
021    import java.util.HashMap;
022    import java.util.Map;
023    import java.util.UUID;
024    
025    import org.apache.log4j.Logger;
026    
027    import org.apache.wiki.rpc.RPCCallable;
028    import org.apache.wiki.rpc.json.JSONRPCManager;
029    
030    /**
031     *  Manages progressing items.  In general this class is used whenever JSPWiki
032     *  is doing something which may require a long time.  In addition, this manager
033     *  provides a JSON interface for finding remotely what the progress is.  The
034     *  JSON object name is JSON_PROGRESSTRACKER = "{@value #JSON_PROGRESSTRACKER}".
035     *
036     *  @since  2.6
037     */
038    // FIXME: Needs synchronization, I think
039    public class ProgressManager
040    {
041        private Map<String,ProgressItem> m_progressingTasks = new HashMap<String,ProgressItem>();
042    
043        /**
044         *  The name of the progress tracker JSON object.  The current value is "{@value}",
045         */
046        public static final String JSON_PROGRESSTRACKER = "progressTracker";
047    
048        private static Logger log = Logger.getLogger( ProgressManager.class );
049    
050        /**
051         *  Creates a new ProgressManager.
052         */
053        public ProgressManager()
054        {
055            JSONRPCManager.registerGlobalObject( JSON_PROGRESSTRACKER, new JSONTracker() );
056        }
057    
058        /**
059         *  You can use this to get an unique process identifier.
060         *  @return A new random value
061         */
062        public String getNewProgressIdentifier()
063        {
064            return UUID.randomUUID().toString();
065        }
066    
067        /**
068         *  Call this method to get your ProgressItem into the ProgressManager queue.
069         *  The ProgressItem will be moved to state STARTED.
070         *
071         *  @param pi ProgressItem to start
072         *  @param id The progress identifier
073         */
074        public void startProgress( ProgressItem pi, String id )
075        {
076            log.debug("Adding "+id+" to progress queue");
077            m_progressingTasks.put( id, pi );
078            pi.setState( ProgressItem.STARTED );
079        }
080    
081        /**
082         *  Call this method to remove your ProgressItem from the queue (after which
083         *  getProgress() will no longer find it.  The ProgressItem will be moved to state
084         *  STOPPED.
085         *
086         *  @param id The progress identifier
087         */
088        public void stopProgress( String id )
089        {
090            log.debug("Removed "+id+" from progress queue");
091            ProgressItem pi = m_progressingTasks.remove( id );
092            if( pi != null ) pi.setState( ProgressItem.STOPPED );
093        }
094    
095        /**
096         *  Get the progress in percents.
097         *
098         *  @param id The progress identifier.
099         *  @return a value between 0 to 100 indicating the progress
100         *  @throws IllegalArgumentException If no such progress item exists.
101         */
102        public int getProgress( String id )
103            throws IllegalArgumentException
104        {
105            ProgressItem pi = m_progressingTasks.get( id );
106    
107            if( pi != null )
108            {
109                return pi.getProgress();
110            }
111    
112            throw new IllegalArgumentException("No such id was found");
113        }
114    
115        /**
116         *  Provides access to a progress indicator, assuming you know the ID.
117         *  Progress of zero (0) means that the progress has just started, and a progress of
118         *  100 means that it is complete.
119         */
120        public class JSONTracker implements RPCCallable
121        {
122            /**
123             *  Returns upload progress in percents so far.
124             *  @param progressId The string representation of the progress ID that you want to know the
125             *                    progress of.
126             *  @return a value between 0 to 100 indicating the progress
127             */
128            public int getProgress( String progressId )
129            {
130                return ProgressManager.this.getProgress( progressId );
131            }
132        }
133    }