Changeset 96 for java/main/src/main/java/com/framsticks/util
- Timestamp:
- 07/04/13 20:29:50 (11 years ago)
- Location:
- java/main/src/main/java/com/framsticks/util
- Files:
-
- 5 added
- 17 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/util/FramsticksException.java
r88 r96 35 35 } 36 36 37 @Override 38 public String getMessage() { 39 StringBuilder b = new StringBuilder(); 37 public void getShortMessage(StringBuilder b) { 40 38 if (message != null) { 41 39 b.append(message); 40 } else { 41 b.append("?"); 42 42 } 43 43 if (arguments != null) { … … 45 45 b.append(" "); 46 46 } 47 Delimeted d = new Delimeted(", ", "");47 Delimeted<Pair<String, Object>> d = new Delimeted<>(", ", ""); 48 48 d.append(arguments.iterator()); 49 49 50 50 b.append("(").append(d.build()).append(")"); 51 51 } 52 if (this.getCause() != null) { 53 b.append(" caused by: [").append(this.getCause().getMessage()).append("]"); 52 } 53 54 public String getMsg() { 55 return message; 56 } 57 58 @Override 59 public String getMessage() { 60 StringBuilder b = new StringBuilder(); 61 getShortMessage(b); 62 Throwable cause = this.getCause(); 63 while (cause != null) { 64 b.append(" caused by: [").append(cause.getClass().getCanonicalName()).append(": "); 65 if (cause instanceof FramsticksException) { 66 ((FramsticksException) cause).getShortMessage(b); 67 } else { 68 b.append(cause.getMessage()); 69 } 70 b.append("]"); 71 cause = cause.getCause(); 54 72 } 55 73 return b.toString(); -
java/main/src/main/java/com/framsticks/util/Logging.java
r90 r96 2 2 3 3 import org.apache.log4j.Logger; 4 5 import com.framsticks.util.dispatching.ExceptionResultHandler; 4 6 5 7 /** … … 17 19 return false; 18 20 } 21 22 public static ExceptionResultHandler logger(final Logger logger, final String action, final Object subject) { 23 return new ExceptionResultHandler() { 24 @Override 25 public void handle(FramsticksException e) { 26 Logging.log(logger, action, subject, e); 27 } 28 }; 29 } 19 30 } -
java/main/src/main/java/com/framsticks/util/PeriodicTask.java
r90 r96 21 21 22 22 public void again() { 23 dispatcher.dispatch(new Task<C>( System.currentTimeMillis() +period) {23 dispatcher.dispatch(new Task<C>(period) { 24 24 @Override 25 25 public void run() { -
java/main/src/main/java/com/framsticks/util/StateFunctor.java
r85 r96 1 1 package com.framsticks.util; 2 3 import com.framsticks.util.dispatching.ExceptionResultHandler; 2 4 3 5 /** 4 6 * @author Piotr Sniegowski 5 7 */ 6 public interface StateFunctor {8 public interface StateFunctor extends ExceptionResultHandler { 7 9 //TODO RunAt 8 public void call( Exception e);10 public void call(); 9 11 } -
java/main/src/main/java/com/framsticks/util/UnsupportedOperationException.java
r84 r96 4 4 * @author Piotr Sniegowski 5 5 */ 6 public class UnsupportedOperationException extends Exception {6 public class UnsupportedOperationException extends FramsticksException { 7 7 8 8 /** -
java/main/src/main/java/com/framsticks/util/dispatching/AbstractJoinable.java
r90 r96 14 14 import com.framsticks.util.FramsticksException; 15 15 16 17 18 16 public abstract class AbstractJoinable implements Joinable { 17 19 18 private static final Logger log = Logger.getLogger(AbstractJoinable.class); 20 21 19 22 20 protected final Set<JoinableParent> owners = new HashSet<JoinableParent>(); … … 25 23 protected static final Set<AbstractJoinable> joinablesRegistry = Collections.synchronizedSet(new HashSet<AbstractJoinable>()); 26 24 27 JoinableState state = JoinableState.INITILIAZED; 25 protected JoinableState state = JoinableState.INITILIAZED; 26 protected JoinableParent parent; 27 protected Monitor monitor; 28 28 29 29 /** … … 53 53 this.state = state; 54 54 55 synchronized (this) { 56 log.debug(this + " is notifying " + joinableListeners.size() + " parents"); 57 58 List<JoinableParent> parents = new LinkedList<>(); 59 CollectionUtils.addAll(parents, joinableListeners.iterator()); 60 61 for (JoinableParent p : parents) { 62 if (p != null) { 63 Dispatching.childChangedState(p, this, state); 64 } 65 } 66 this.notifyAll(); 67 } 55 log.debug(this + " is notifying " + joinableListeners.size() + " parents"); 56 57 List<JoinableParent> parents = new LinkedList<>(); 58 CollectionUtils.addAll(parents, joinableListeners.iterator()); 59 60 for (JoinableParent p : parents) { 61 if (p != null) { 62 Dispatching.childChangedState(p, this, state); 63 } 64 } 65 this.notifyAll(); 66 68 67 report(); 69 68 … … 145 144 } 146 145 if (start) { 146 assert monitor == null; 147 monitor = owner.getMonitor(); 147 148 return this.start(); 148 149 } … … 185 186 } 186 187 188 protected boolean isRunning() { 189 return state.equals(JoinableState.RUNNING); 190 } 191 192 @Override 193 public String toString() { 194 return getName(); 195 } 196 197 // @Override 198 public Monitor getMonitor() { 199 return monitor; 200 } 201 187 202 188 203 } -
java/main/src/main/java/com/framsticks/util/dispatching/Dispatching.java
r90 r96 3 3 import org.apache.log4j.Logger; 4 4 5 import com.framsticks.util.FramsticksException; 5 6 import com.framsticks.util.StateFunctor; 6 7 … … 28 29 @Override 29 30 public void run() { 30 stateFunctor.call( null);31 stateFunctor.call(); 31 32 } 32 33 }); … … 170 171 } 171 172 172 173 public static class Waiter { 174 protected boolean done = false; 175 176 protected final double timeOut; 177 178 /** 179 * @param timeOut 180 */ 181 public Waiter(double timeOut) { 182 this.timeOut = timeOut; 183 } 184 185 public synchronized void pass() { 186 done = true; 187 this.notify(); 188 } 189 190 public synchronized void waitFor() { 191 long end = System.currentTimeMillis() + (int)(timeOut * 1000); 192 while ((!done) && System.currentTimeMillis() < end) { 193 try { 194 this.wait(end - System.currentTimeMillis()); 195 } catch (InterruptedException e) { 196 break; 197 } 198 } 199 if (!done) { 200 throw new FramsticksException().msg("waiter timed out"); 201 } 202 } 203 } 204 205 206 public static <C> void synchronize(Dispatcher<C> dispatcher, double seconds) { 207 final Waiter waiter = new Waiter(seconds); 208 dispatcher.dispatch(new RunAt<C>() { 209 @Override 210 public void run() { 211 waiter.pass(); 212 } 213 }); 214 waiter.waitFor(); 215 } 173 216 174 217 } -
java/main/src/main/java/com/framsticks/util/dispatching/Future.java
r84 r96 1 1 package com.framsticks.util.dispatching; 2 3 import com.framsticks.util.FramsticksException; 2 4 3 5 /** 4 6 * @author Piotr Sniegowski 5 7 */ 6 public interface Future <T> { 7 void result(T result, Exception e); 8 public abstract class Future<T> implements ExceptionResultHandler { 9 10 protected final ExceptionResultHandler handler; 11 12 public Future(ExceptionResultHandler handler) { 13 this.handler = handler; 14 } 15 16 public Future() { 17 this.handler = null; 18 } 19 20 protected abstract void result(T result); 21 22 public final void pass(T result) { 23 try { 24 result(result); 25 } catch (FramsticksException e) { 26 handle(e); 27 } 28 } 29 30 public static <T> void passOrHandle(Future<T> future, T value, FramsticksException e) { 31 if (e != null) { 32 future.handle(e); 33 } else { 34 future.pass(value); 35 } 36 } 37 38 @Override 39 public void handle(FramsticksException exception) { 40 if (handler != null) { 41 handler.handle(exception); 42 return; 43 } 44 throw exception; 45 } 8 46 } -
java/main/src/main/java/com/framsticks/util/dispatching/Joinable.java
r88 r96 5 5 6 6 public interface Joinable { 7 8 /** The name of the joinable. 9 * 10 * It should never be empty, but it is not required to remain 11 * constant during lifetime of the object. 12 */ 13 public String getName(); 7 14 8 15 public boolean use(@Nonnull JoinableParent owner); -
java/main/src/main/java/com/framsticks/util/dispatching/JoinableCollection.java
r88 r96 2 2 3 3 import java.util.Collections; 4 import java.util.HashMap;5 4 import java.util.HashSet; 6 5 import java.util.Iterator; 7 import java.util.Map;8 6 import java.util.Set; 9 7 10 import com.framsticks.core.Entity;11 8 import com.framsticks.params.annotations.AutoAppendAnnotation; 12 9 import com.framsticks.params.annotations.FramsClassAnnotation; … … 19 16 20 17 protected final Set<T> joinables = new HashSet<T>(); 21 protected final Map<String, T> namedJoinables = new HashMap<>();22 18 23 19 protected boolean finishIfOne; … … 34 30 35 31 @AutoAppendAnnotation 36 public void add(T joinable) { 37 assert isInState(JoinableState.INITILIAZED); 32 public synchronized void add(T joinable) { 33 if (this.state.ordinal() > JoinableState.RUNNING.ordinal()) { 34 throw new FramsticksException().msg("failed to add joinable - collection is passed running state").arg("joinable", joinable).arg("collection", this); 35 } 38 36 39 37 if (joinables.contains(joinable)) { 40 38 throw new FramsticksException().msg("joinable is already observed").arg("joinable", joinable).arg("in", this); 41 39 } 42 // if (observables.containsKey(observable.getName())) { 43 // throw new FramsticksException().msg("observable with given name already exists").arg("name", observable.getName()).arg("in", this); 44 // } 40 joinables.add(joinable); 45 41 46 if (joinable instanceof Entity) { 47 Entity e = (Entity) joinable; 48 if (!namedJoinables.containsKey(e.getName())) { 49 namedJoinables.put(e.getName(), joinable); 50 } 42 if (this.state.equals(JoinableState.RUNNING)) { 43 Dispatching.use(joinable, this); 51 44 } 52 53 joinables.add(joinable);54 45 } 55 46 … … 107 98 public void childChangedState(Joinable joinable, JoinableState state) { 108 99 proceedToState(getNextState()); 109 110 100 } 111 101 … … 129 119 130 120 public T get(String name) { 131 return namedJoinables.get(name); 121 for (T j : joinables) { 122 if (j.getName().equals(name)) { 123 return j; 124 } 125 } 126 return null; 132 127 } 133 128 129 public int size() { 130 return joinables.size(); 131 } 134 132 135 public Map<String, T> getObservables() { 136 return Collections.unmodifiableMap(namedJoinables); 133 public boolean contains(T joinable) { 134 return joinables.contains(joinable); 135 } 136 137 @Override 138 public String getName() { 139 return observableName; 137 140 } 138 141 -
java/main/src/main/java/com/framsticks/util/dispatching/JoinableParent.java
r88 r96 4 4 5 5 public void childChangedState(Joinable joinable, JoinableState state); 6 public Monitor getMonitor(); 6 7 7 8 } -
java/main/src/main/java/com/framsticks/util/dispatching/Monitor.java
r90 r96 2 2 3 3 import org.apache.log4j.Logger; 4 // import edu.umd.cs.findbugs.annotations.SuppressWarnings;5 4 import com.framsticks.util.dispatching.Dispatching; 5 import java.lang.Thread; 6 6 7 7 public class Monitor implements JoinableParent { … … 10 10 11 11 protected final Joinable joinable; 12 protected final Thread shutdownHook; 12 13 13 14 /** … … 16 17 public Monitor(Joinable joinable) { 17 18 this.joinable = joinable; 19 20 shutdownHook = new Thread(new Runnable() { 21 @Override 22 public void run() { 23 log.debug("running shutdown hook"); 24 Monitor.this.drop().join(); 25 } 26 }); 27 } 28 29 public Monitor use() { 30 Runtime.getRuntime().addShutdownHook(shutdownHook); 31 32 log.debug(this + " is using"); 33 Dispatching.use(joinable, this); 34 return this; 18 35 } 19 36 … … 34 51 } 35 52 36 public Monitor use() {37 log.debug(this + " is using");38 Dispatching.use(joinable, this);39 return this;40 }41 53 42 54 public Monitor drop() { … … 50 62 Dispatching.joinAbsolutely(joinable); 51 63 log.debug(this + " is joined"); 64 65 try { 66 Runtime.getRuntime().removeShutdownHook(shutdownHook); 67 } catch (IllegalStateException e) { 68 /** In case IllegalStateException is caught, it means that JVM is in finalization stage */ 69 } 70 52 71 return this; 53 72 } … … 67 86 } 68 87 88 @Override 89 public Monitor getMonitor() { 90 return this; 91 } 92 93 protected ExceptionHandler taskExceptionHandler; 94 95 /** 96 * @return the taskExceptionHandler 97 */ 98 public ExceptionHandler getTaskExceptionHandler() { 99 return taskExceptionHandler; 100 } 101 102 /** 103 * @param taskExceptionHandler the taskExceptionHandler to set 104 */ 105 public void setTaskExceptionHandler(ExceptionHandler taskExceptionHandler) { 106 this.taskExceptionHandler = taskExceptionHandler; 107 } 108 69 109 } -
java/main/src/main/java/com/framsticks/util/dispatching/Task.java
r85 r96 13 13 14 14 public Task(long moment) { 15 this.moment = moment;15 this.moment = System.currentTimeMillis() + moment; 16 16 } 17 17 -
java/main/src/main/java/com/framsticks/util/dispatching/Thread.java
r90 r96 47 47 protected void routine() { 48 48 log.debug("starting thread " + this); 49 assert getMonitor() != null; 50 ExceptionHandler exceptionHandler = getMonitor().getTaskExceptionHandler(); 49 51 while (!java.lang.Thread.interrupted()) { 50 52 Task<? extends C> task; … … 73 75 task.run(); 74 76 } catch (Exception e) { 77 if (exceptionHandler != null) { 78 if (exceptionHandler.handle(this, e)) { 79 continue; 80 } 81 } 75 82 log.error("error in thread: ", e); 76 83 } -
java/main/src/main/java/com/framsticks/util/lang/Delimeted.java
r86 r96 3 3 import java.util.Iterator; 4 4 5 import org.apache.commons.collections.Closure;5 // import org.apache.commons.collections.Closure; 6 6 7 public class Delimeted implements Closure{7 public class Delimeted<T> { 8 8 9 9 protected final String delimeter; … … 19 19 } 20 20 21 public final Delimeted append(Objectobject) {21 public final Delimeted<T> append(T object) { 22 22 if (builder != null) { 23 23 builder.append(delimeter); … … 29 29 } 30 30 31 public final Delimeted append(Iterator<?> i) {31 public final Delimeted<T> append(Iterator<T> i) { 32 32 while (i.hasNext()) { 33 33 append(i.next()); … … 41 41 42 42 @Override 43 public void execute(Object input) {44 append(input);43 public String toString() { 44 return build(); 45 45 } 46 46 47 // @Override 48 // public void execute(Object input) { 49 // append(input); 50 // } 51 47 52 } -
java/main/src/main/java/com/framsticks/util/lang/Pair.java
r87 r96 24 24 } 25 25 Pair<?, ?> p = (Pair<?, ?>) obj; 26 // return first == p.first && second.equals(se)27 26 return first.equals(p.first) && second.equals(p.second); 28 27 } … … 33 32 } 34 33 34 public static <U1, U2> Pair<U1, U2> make(U1 first, U2 second) { 35 return new Pair<U1, U2>(first, second); 36 } 37 35 38 } -
java/main/src/main/java/com/framsticks/util/lang/Strings.java
r86 r96 1 1 package com.framsticks.util.lang; 2 3 import com.framsticks.util.FramsticksException; 2 4 3 5 /** … … 15 17 public static boolean notEmpty(String str) { 16 18 return str != null && !str.equals(""); 19 } 20 21 public static void assureNotEmpty(String str) { 22 if (!notEmpty(str)) { 23 throw new FramsticksException().msg("string is empty"); 24 } 17 25 } 18 26 … … 35 43 int pos = string.indexOf(separator); 36 44 if (pos == -1) { 37 return new Pair<String, String>(string.substring(0, 38 string.length() - 1), second); 45 return new Pair<String, String>(string, second); 39 46 } else { 40 return new Pair<String, String>(string.substring(0, pos), 41 string.substring(pos + 1)); 47 return new Pair<String, String>(string.substring(0, pos), string.substring(pos + 1)); 42 48 } 43 49 }
Note: See TracChangeset
for help on using the changeset viewer.