001 /*
002 * JBoss, Home of Professional Open Source.
003 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004 * as indicated by the @author tags. See the copyright.txt file in the
005 * distribution for a full listing of individual contributors.
006 *
007 * This is free software; you can redistribute it and/or modify it
008 * under the terms of the GNU Lesser General Public License as
009 * published by the Free Software Foundation; either version 2.1 of
010 * the License, or (at your option) any later version.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022
023 package org.jboss.dna.common.monitor;
024
025 import java.util.Locale;
026 import java.util.concurrent.locks.ReadWriteLock;
027 import java.util.concurrent.locks.ReentrantReadWriteLock;
028 import net.jcip.annotations.GuardedBy;
029 import org.jboss.dna.common.collection.Problems;
030 import org.jboss.dna.common.i18n.I18n;
031
032 /**
033 * This class is thread-safe except when accessing or adding {@link #getProblems() problems}. Problems must only be added by the
034 * {@link ProgressMonitor <strong>Updater</strong>}, and accessed by {@link ProgressMonitor Observers} only after the activity
035 * has been {@link #done() completed}.
036 *
037 * @author Randall Hauch
038 */
039 public class SubProgressMonitor implements ProgressMonitor {
040
041 @GuardedBy( "lock" )
042 private I18n taskName;
043 @GuardedBy( "lock" )
044 private Object[] params;
045 @GuardedBy( "lock" )
046 private double totalWork;
047 @GuardedBy( "lock" )
048 private double parentWorkScaleFactor;
049 @GuardedBy( "lock" )
050 private double submittedToParent;
051
052 private final double subtaskTotalInParent;
053 private final ProgressMonitor parent;
054 private final ReadWriteLock lock = new ReentrantReadWriteLock();
055
056 public SubProgressMonitor( final ProgressMonitor parent,
057 final double subtaskTotalInParent ) {
058 assert subtaskTotalInParent > 0;
059 assert parent != null;
060 this.parent = parent;
061 this.subtaskTotalInParent = subtaskTotalInParent;
062 }
063
064 /**
065 * {@inheritDoc}
066 */
067 public String getActivityName() {
068 return this.parent.getActivityName();
069 }
070
071 /**
072 * @return parent
073 */
074 public ProgressMonitor getParent() {
075 return this.parent;
076 }
077
078 /**
079 * {@inheritDoc}
080 */
081 public void beginTask( double totalWork,
082 I18n name,
083 Object... params ) {
084 assert totalWork > 0;
085 try {
086 this.lock.writeLock().lock();
087 this.taskName = name;
088 this.params = params;
089 this.totalWork = totalWork;
090 this.parentWorkScaleFactor = ((float)subtaskTotalInParent) / ((float)totalWork);
091 } finally {
092 this.lock.writeLock().unlock();
093 }
094 }
095
096 /**
097 * {@inheritDoc}
098 */
099 public ProgressMonitor createSubtask( double subtaskWork ) {
100 return new SubProgressMonitor(this, subtaskWork);
101 }
102
103 /**
104 * {@inheritDoc}
105 */
106 public void done() {
107 // Compute the total work for this task in terms of the parent ...
108 double workInParentRemaining = 0.0d;
109 try {
110 this.lock.writeLock().lock();
111 double totalWorkInParent = this.totalWork * this.parentWorkScaleFactor;
112 workInParentRemaining = totalWorkInParent - this.submittedToParent;
113 } finally {
114 this.lock.writeLock().unlock();
115 }
116 // Don't do this in the lock: it's incremental, but doing so might cause deadlock with future changes
117 this.parent.worked(workInParentRemaining);
118 }
119
120 /**
121 * {@inheritDoc}
122 */
123 public boolean isCancelled() {
124 return this.parent.isCancelled();
125 }
126
127 /**
128 * {@inheritDoc}
129 *
130 * @see org.jboss.dna.common.monitor.ProgressMonitor#isDone()
131 */
132 public boolean isDone() {
133 return parent.isDone();
134 }
135
136 /**
137 * {@inheritDoc}
138 */
139 public void setCancelled( boolean value ) {
140 this.parent.setCancelled(value);
141 }
142
143 /**
144 * {@inheritDoc}
145 */
146 public void worked( double work ) {
147 if (this.isCancelled()) return;
148 if (work > 0) {
149 double workInParent = 0.0d;
150 try {
151 this.lock.writeLock().lock();
152 workInParent = work * parentWorkScaleFactor;
153 this.submittedToParent += workInParent;
154 } finally {
155 this.lock.writeLock().unlock();
156 }
157 // Don't do this in the lock: it's incremental, but doing so might cause deadlock with future changes
158 this.parent.worked(workInParent);
159 }
160 }
161
162 /**
163 * {@inheritDoc}
164 */
165 public ProgressStatus getStatus( Locale locale ) {
166 try {
167 this.lock.readLock().lock();
168 return new ProgressStatus(this.getActivityName(), this.taskName.text(locale, this.params), this.submittedToParent,
169 this.subtaskTotalInParent, this.isCancelled());
170 } finally {
171 this.lock.readLock().unlock();
172 }
173 }
174
175 /**
176 * {@inheritDoc}
177 *
178 * @see org.jboss.dna.common.monitor.ProgressMonitor#getProblems()
179 */
180 public Problems getProblems() {
181 return parent.getProblems();
182 }
183 }