* added IntervalWorker

* added CustomAppender for logging
* minor modifications
This commit is contained in:
2013-07-30 10:45:27 +02:00
parent 23634cf00e
commit a918758ee3
16 changed files with 743 additions and 638 deletions

View File

@@ -10,5 +10,6 @@
<classpathentry kind="lib" path="wiiusej.jar"/> <classpathentry kind="lib" path="wiiusej.jar"/>
<classpathentry kind="lib" path="lib/commons-collections-3.2.1.jar"/> <classpathentry kind="lib" path="lib/commons-collections-3.2.1.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View File

@@ -1,4 +1,8 @@
log4j.rootLogger=TRACE, CA log4j.rootLogger=TRACE, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.CUSTOM=test.CustomAppender
log4j.appender.CUSTOM.Bla=1234
log4j.appender.CUSTOM.layout=org.apache.log4j.PatternLayout
log4j.appender.CUSTOM.layout.ConversionPattern=[%d{MMM dd HH:mm:ss}] %-5p (%F:%L) - %m%n

Binary file not shown.

Binary file not shown.

BIN
java/mimis.exe Normal file

Binary file not shown.

View File

@@ -1,24 +1,25 @@
package mimis; package mimis;
import mimis.exception.worker.ActivateException; import mimis.exception.worker.ActivateException;
import mimis.util.swing.Dialog; import mimis.router.GlobalRouter;
import mimis.util.swing.Dialog;
public class Client extends Main {
public static final String IP = "127.0.0.1"; public class Client extends Main {
public static final int PORT = 6789; public static final String IP = "127.0.0.1";
public static final int PORT = 6789;
public Client(String ip, int port) {
super(); public Client(String ip, int port) {
//eventRouter = new GlobalRouter(ip, port); super();
} router = new GlobalRouter(ip, port);
}
public void activate() throws ActivateException {
super.activate(); public void activate() throws ActivateException {
} super.activate();
}
public static void main(String[] args) {
String ip = Dialog.question("Server IP:", IP); public static void main(String[] args) {
int port = Integer.valueOf(Dialog.question("Server Port:", PORT)); String ip = Dialog.question("Server IP:", IP);
new Client(ip, port).start(); int port = Integer.valueOf(Dialog.question("Server Port:", PORT));
} new Client(ip, port).start();
}
} }

View File

@@ -5,8 +5,6 @@ import java.util.ServiceLoader;
import javax.swing.UIManager; import javax.swing.UIManager;
import mimis.application.Application;
import mimis.device.Device;
import mimis.exception.worker.ActivateException; import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException; import mimis.exception.worker.DeactivateException;
import mimis.input.Task; import mimis.input.Task;
@@ -28,20 +26,18 @@ public class Main extends Mimis {
} }
public static Component[] getApplications() { public static Component[] getApplications() {
ArrayList<Component> componentList = new ArrayList<Component>(); return getComponents(mimis.application.Application.class);
for (Application application : ServiceLoader.load(mimis.application.Application.class)) {
if (application instanceof Component) {
componentList.add((Component) application);
}
}
return componentList.toArray(new Component[]{});
} }
public static Component[] getDevices() { public static Component[] getDevices() {
return getComponents(mimis.device.Device.class);
}
public static Component[] getComponents(Class<?> clazz) {
ArrayList<Component> componentList = new ArrayList<Component>(); ArrayList<Component> componentList = new ArrayList<Component>();
for (Device device : ServiceLoader.load(mimis.device.Device.class)) { for (Object object : ServiceLoader.load(clazz)) {
if (device instanceof Component) { if (object instanceof Component) {
componentList.add((Component) device); componentList.add((Component) object);
} }
} }
return componentList.toArray(new Component[]{}); return componentList.toArray(new Component[]{});
@@ -95,6 +91,8 @@ public class Main extends Mimis {
case PREVIOUS: case PREVIOUS:
applicationManager.currentChanged(); applicationManager.currentChanged();
break; break;
default:
break;
} }
} }

View File

@@ -1,79 +1,81 @@
package mimis; package mimis;
import mimis.exception.worker.ActivateException; import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException; import mimis.exception.worker.DeactivateException;
import mimis.input.Feedback; import mimis.input.Feedback;
import mimis.input.Task; import mimis.input.Task;
import mimis.manager.Manager; import mimis.manager.Manager;
import mimis.parser.Parser; import mimis.parser.Parser;
import mimis.router.Router; import mimis.router.Router;
import mimis.util.ArrayCycle; import mimis.util.ArrayCycle;
import mimis.value.Action; import mimis.value.Action;
import mimis.value.Target; import mimis.value.Target;
import mimis.worker.Component; import mimis.worker.Component;
public abstract class Mimis extends Component { public abstract class Mimis extends Component {
protected Component[] currentArray; protected Component[] currentArray;
protected Manager manager; protected Manager manager;
protected ArrayCycle<Component> componentCycle; protected ArrayCycle<Component> componentCycle;
public Mimis(Component... currentArray) { public Mimis(Component... currentArray) {
this.currentArray = initialize(false, currentArray); this.currentArray = initialize(false, currentArray);
componentCycle = new ArrayCycle<Component>(currentArray); componentCycle = new ArrayCycle<Component>(currentArray);
router = new Router(); router = new Router();
manager = new Manager(initialize(true, router, new Parser())); manager = new Manager(initialize(true, router, new Parser()));
} }
public void activate() throws ActivateException { public void activate() throws ActivateException {
manager.start(); manager.start();
super.activate(); super.activate();
} }
protected void deactivate() throws DeactivateException { protected void deactivate() throws DeactivateException {
super.deactivate(); super.deactivate();
manager.stop(); manager.stop();
} }
public void exit() { public void exit() {
super.exit(); super.exit();
manager.exit(); manager.exit();
} }
public Component[] initialize(boolean start, Component... componentArray) { public Component[] initialize(boolean start, Component... componentArray) {
for (Component component : componentArray) { for (Component component : componentArray) {
component.setRouter(router); component.setRouter(router);
if (start) { if (start) {
component.start(); component.start();
} }
} }
return componentArray; return componentArray;
} }
public void task(Task task) { public void task(Task task) {
if (task.getTarget().equals(Target.CURRENT)) { if (task.getTarget().equals(Target.CURRENT)) {
componentCycle.current().add(task); componentCycle.current().add(task);
} else { } else {
super.task(task); super.task(task);
} }
} }
public void end(Action action) { public void end(Action action) {
switch (action) { switch (action) {
case CURRENT: case CURRENT:
route(new Feedback("Current component: " + componentCycle.current().getTitle())); route(new Feedback("Current component: " + componentCycle.current().getTitle()));
break; break;
case NEXT: case NEXT:
log.debug("Next component"); log.debug("Next component");
route(new Feedback("Next component: " + componentCycle.next().getTitle())); route(new Feedback("Next component: " + componentCycle.next().getTitle()));
break; break;
case PREVIOUS: case PREVIOUS:
log.debug("Previous component"); log.debug("Previous component");
route(new Feedback("Previous component: " + componentCycle.previous().getTitle())); route(new Feedback("Previous component: " + componentCycle.previous().getTitle()));
break; break;
case EXIT: case EXIT:
exit(); exit();
break; break;
} default:
} break;
} }
}
}

View File

@@ -1,177 +1,177 @@
package mimis.application.itunes; package mimis.application.itunes;
import mimis.application.Application; import mimis.application.Application;
import mimis.exception.worker.ActivateException; import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException; import mimis.exception.worker.DeactivateException;
import mimis.value.Action; import mimis.value.Action;
import mimis.worker.Component; import mimis.worker.Component;
import mimis.worker.Worker; import mimis.worker.Worker;
import com.dt.iTunesController.ITCOMDisabledReason; import com.dt.iTunesController.ITCOMDisabledReason;
import com.dt.iTunesController.ITTrack; import com.dt.iTunesController.ITTrack;
import com.dt.iTunesController.iTunes; import com.dt.iTunesController.iTunes;
import com.dt.iTunesController.iTunesEventsInterface; import com.dt.iTunesController.iTunesEventsInterface;
public class iTunesApplication extends Component implements Application, iTunesEventsInterface { public class iTunesApplication extends Component implements Application, iTunesEventsInterface {
protected static final String TITLE = "iTunes"; protected static final String TITLE = "iTunes";
protected static final boolean EVENTS = false; protected static final boolean EVENTS = false;
protected static final int VOLUME_CHANGE_RATE = 5; protected static final int VOLUME_CHANGE_RATE = 5;
protected static final int VOLUME_SLEEP = 100; protected static final int VOLUME_SLEEP = 100;
protected static final String PLAYLIST_LIKE = "Like"; protected static final String PLAYLIST_LIKE = "Like";
protected static final String PLAYLIST_DISLIKE = "Dislike"; protected static final String PLAYLIST_DISLIKE = "Dislike";
protected iTunes iTunes; protected iTunes iTunes;
protected VolumeWorker volumeWorker; protected VolumeWorker volumeWorker;
protected boolean events; protected boolean events;
public iTunesApplication() { public iTunesApplication() {
this(EVENTS); this(EVENTS);
} }
public iTunesApplication(boolean events) { public iTunesApplication(boolean events) {
super(TITLE); super(TITLE);
this.events = events; this.events = events;
volumeWorker = new VolumeWorker(); volumeWorker = new VolumeWorker();
} }
protected synchronized void activate() throws ActivateException { protected synchronized void activate() throws ActivateException {
iTunes = new iTunes(); iTunes = new iTunes();
iTunes.connect(); iTunes.connect();
if (events) { if (events) {
iTunes.addEventHandler(this); iTunes.addEventHandler(this);
} }
super.activate(); super.activate();
} }
public synchronized boolean active() { public synchronized boolean active() {
try { try {
iTunes.getMute(); iTunes.getMute();
active = true; active = true;
} catch (Exception e) { } catch (Exception e) {
active = false; active = false;
} }
return active; return active;
} }
protected synchronized void deactivate() throws DeactivateException { protected synchronized void deactivate() throws DeactivateException {
if (events) { if (events) {
exit(); exit();
} else { } else {
super.deactivate(); super.deactivate();
volumeWorker.stop(); volumeWorker.stop();
try { try {
iTunes.release(); iTunes.release();
} catch (Exception e) { } catch (Exception e) {
log.error(e); log.error(e);
throw new DeactivateException(); throw new DeactivateException();
} }
} }
} }
public synchronized void exit() { public synchronized void exit() {
try { try {
iTunes.quit(); iTunes.quit();
} catch (Exception e) {} } catch (Exception e) {}
volumeWorker.exit(); volumeWorker.exit();
super.exit(); super.exit();
} }
protected void begin(Action action) { protected void begin(Action action) {
log.trace("iTunesApplication begin: " + action); log.trace("iTunesApplication begin: " + action);
if (!active) return; if (!active) return;
switch (action) { switch (action) {
case FORWARD: case FORWARD:
iTunes.fastForward(); iTunes.fastForward();
break; break;
case REWIND: case REWIND:
iTunes.rewind(); iTunes.rewind();
break; break;
case VOLUME_UP: case VOLUME_UP:
volumeWorker.start(VOLUME_CHANGE_RATE); volumeWorker.start(VOLUME_CHANGE_RATE);
break; break;
case VOLUME_DOWN: case VOLUME_DOWN:
volumeWorker.start(-VOLUME_CHANGE_RATE); volumeWorker.start(-VOLUME_CHANGE_RATE);
break; break;
} }
} }
protected void end(Action action) { protected void end(Action action) {
log.trace("iTunesApplication end: " + action); log.trace("iTunesApplication end: " + action);
if (!active) return; if (!active) return;
switch (action) { switch (action) {
case PLAY: case PLAY:
iTunes.playPause(); iTunes.playPause();
break; break;
case NEXT: case NEXT:
iTunes.nextTrack(); iTunes.nextTrack();
break; break;
case PREVIOUS: case PREVIOUS:
iTunes.previousTrack(); iTunes.previousTrack();
break; break;
case FORWARD: case FORWARD:
iTunes.resume(); iTunes.resume();
break; break;
case REWIND: case REWIND:
iTunes.resume(); iTunes.resume();
break; break;
case MUTE: case MUTE:
iTunes.toggleMute(); iTunes.toggleMute();
break; break;
case VOLUME_UP: case VOLUME_UP:
case VOLUME_DOWN: case VOLUME_DOWN:
volumeWorker.stop(); volumeWorker.stop();
break; break;
case SHUFFLE: case SHUFFLE:
iTunes.toggleShuffle(); iTunes.toggleShuffle();
break; break;
case REPEAT: case REPEAT:
iTunes.cycleSongRepeat(); iTunes.cycleSongRepeat();
break; break;
case LIKE: case LIKE:
iTunes.playlistAddCurrentTrack(PLAYLIST_LIKE); iTunes.playlistAddCurrentTrack(PLAYLIST_LIKE);
break; break;
case DISLIKE: case DISLIKE:
iTunes.playlistAddCurrentTrack(PLAYLIST_DISLIKE); iTunes.playlistAddCurrentTrack(PLAYLIST_DISLIKE);
break; break;
} }
} }
protected int getVolume() { protected int getVolume() {
return iTunes.getSoundVolume(); return iTunes.getSoundVolume();
} }
public void onDatabaseChangedEvent(int[][] deletedObjectIDs, int[][] changedObjectIDs) {} public void onDatabaseChangedEvent(int[][] deletedObjectIDs, int[][] changedObjectIDs) {}
public void onPlayerPlayEvent(ITTrack iTrack) { public void onPlayerPlayEvent(ITTrack iTrack) {
if (active) { if (active) {
log.trace("iTunesEvent: play"); log.trace("iTunesEvent: play");
} }
} }
public void onPlayerStopEvent(ITTrack iTrack) { public void onPlayerStopEvent(ITTrack iTrack) {
if (active) { if (active) {
log.trace("iTunesEvent: stop"); log.trace("iTunesEvent: stop");
} }
} }
public void onPlayerPlayingTrackChangedEvent(ITTrack iTrack) {} public void onPlayerPlayingTrackChangedEvent(ITTrack iTrack) {}
public void onCOMCallsDisabledEvent(ITCOMDisabledReason reason) {} public void onCOMCallsDisabledEvent(ITCOMDisabledReason reason) {}
public void onCOMCallsEnabledEvent() {} public void onCOMCallsEnabledEvent() {}
public void onQuittingEvent() {} public void onQuittingEvent() {}
public void onAboutToPromptUserToQuitEvent() {} public void onAboutToPromptUserToQuitEvent() {}
public void onSoundVolumeChangedEvent(int newVolume) {} public void onSoundVolumeChangedEvent(int newVolume) {}
protected class VolumeWorker extends Worker { protected class VolumeWorker extends Worker {
protected int volumeChangeRate; protected int volumeChangeRate;
public void start(int volumeChangeRate) { public void start(int volumeChangeRate) {
super.start(); super.start();
this.volumeChangeRate = volumeChangeRate; this.volumeChangeRate = volumeChangeRate;
} }
public void work() { public void work() {
iTunes.setSoundVolume(getVolume() + volumeChangeRate); iTunes.setSoundVolume(getVolume() + volumeChangeRate);
sleep(VOLUME_SLEEP); sleep(VOLUME_SLEEP);
} }
}; };
} }

View File

@@ -1,81 +1,76 @@
package mimis.manager; package mimis.manager;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.util.Calendar; import java.util.HashMap;
import java.util.HashMap; import java.util.Map;
import java.util.Map;
import javax.swing.JPanel;
import javax.swing.JPanel; import javax.swing.JToggleButton;
import javax.swing.JToggleButton; import javax.swing.border.TitledBorder;
import javax.swing.border.TitledBorder;
import mimis.worker.Worker;
import mimis.worker.Worker;
public class ButtonManager extends Manager {
public class ButtonManager extends Manager { protected static final String TITLE = "Workers";
protected static final String TITLE = "Workers";
protected String title;
protected String title; protected Map<Worker, WorkerButton> buttonMap;
protected Map<Worker, WorkerButton> buttonMap;
public ButtonManager(Worker... workerArray) {
public ButtonManager(Worker... workerArray) { this(TITLE, workerArray);
this(TITLE, workerArray); }
}
public ButtonManager(String title, Worker... workerArray) {
public ButtonManager(String title, Worker... workerArray) { super(workerArray);
super(workerArray); this.title = title;
this.title = title; createButtons();
createButtons(); }
}
public String getTitle() {
public String getTitle() { return title;
return title; }
}
public WorkerButton[] getButtons() {
public WorkerButton[] getButtons() { return buttonMap.values().toArray(new WorkerButton[]{});
return buttonMap.values().toArray(new WorkerButton[]{}); }
}
protected void createButtons() {
protected void createButtons() { buttonMap = new HashMap<Worker, WorkerButton>();
buttonMap = new HashMap<Worker, WorkerButton>(); for (Worker worker : workerList) {
for (Worker worker : workerList) { WorkerButton button = new WorkerButton(worker);
WorkerButton button = new WorkerButton(worker); buttonMap.put(worker, button);
buttonMap.put(worker, button); }
} }
}
public JPanel createPanel() {
public JPanel createPanel() { /* Initialize components */
/* Initialize components */ GridBagLayout gridBagLayout = new GridBagLayout();
GridBagLayout gridBagLayout = new GridBagLayout(); GridBagConstraints gridBagConstraints = new GridBagConstraints();
GridBagConstraints gridBagConstraints = new GridBagConstraints(); JPanel panel = new JPanel(gridBagLayout);
JPanel panel = new JPanel(gridBagLayout);
/* Set border */
/* Set border */ TitledBorder border = new TitledBorder(getTitle());
TitledBorder border = new TitledBorder(getTitle()); border.setTitleJustification(TitledBorder.CENTER);
border.setTitleJustification(TitledBorder.CENTER); panel.setBorder(border);
panel.setBorder(border);
/* Initialize constraints */
/* Initialize constraints */ gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.fill = GridBagConstraints.BOTH; gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER; gridBagConstraints.weightx = 1;
gridBagConstraints.weightx = 1; gridBagConstraints.weighty = 1;
gridBagConstraints.weighty = 1;
/* Add buttons */
/* Add buttons */ for (JToggleButton button : getButtons()) {
for (JToggleButton button : getButtons()) { gridBagLayout.setConstraints(button, gridBagConstraints);
gridBagLayout.setConstraints(button, gridBagConstraints); panel.add(button);
panel.add(button); }
} return panel;
return panel; }
}
protected void work() {
protected void work() { for (Worker worker : workerList) {
long before = Calendar.getInstance().getTimeInMillis(); buttonMap.get(worker).setPressed(worker.active());
for (Worker worker : workerList) { }
buttonMap.get(worker).setPressed(worker.active()); }
} }
long after = Calendar.getInstance().getTimeInMillis();
int sleep = INTERVAL - (int) (after - before);
sleep(sleep);
}
}

View File

@@ -1,55 +1,51 @@
package mimis.manager; package mimis.manager;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar;
import mimis.exception.worker.DeactivateException;
import mimis.exception.worker.DeactivateException; import mimis.worker.IntervalWorker;
import mimis.worker.Worker; import mimis.worker.Worker;
public class Manager extends Worker { public class Manager extends IntervalWorker {
protected static final int INTERVAL = 1000; protected static final int INTERVAL = 1000;
protected ArrayList<Worker> workerList; protected ArrayList<Worker> workerList;
public Manager(Worker... workerArray) { public Manager(Worker... workerArray) {
workerList = new ArrayList<Worker>(); workerList = new ArrayList<Worker>();
add(workerArray); add(workerArray);
} }
public void add(Worker... workerArray) { public void add(Worker... workerArray) {
workerList.addAll(Arrays.asList(workerArray)); workerList.addAll(Arrays.asList(workerArray));
} }
public void remove(Worker... workerArray) { public void remove(Worker... workerArray) {
workerList.removeAll(Arrays.asList(workerArray)); workerList.removeAll(Arrays.asList(workerArray));
} }
protected void deactivate() throws DeactivateException { protected void deactivate() throws DeactivateException {
super.deactivate(); super.deactivate();
for (Worker worker : workerList) {
worker.stop();
}
}
public void exit() {
super.exit();
for (Worker worker : workerList) { for (Worker worker : workerList) {
worker.exit(); worker.stop();
} }
} }
public int count() { public void exit() {
return workerList.size(); super.exit();
} for (Worker worker : workerList) {
worker.exit();
protected void work() { }
long before = Calendar.getInstance().getTimeInMillis(); }
for (Worker worker : workerList) {
worker.active(); public int count() {
} return workerList.size();
long after = Calendar.getInstance().getTimeInMillis(); }
int sleep = INTERVAL - (int) (after - before);
sleep(sleep); protected void work() {
} for (Worker worker : workerList) {
} worker.active();
}
}
}

View File

@@ -1,90 +1,89 @@
package mimis.router; package mimis.router;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.net.Socket; import java.net.Socket;
import mimis.exception.worker.ActivateException; import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException; import mimis.exception.worker.DeactivateException;
import mimis.input.Feedback; import mimis.input.Feedback;
import mimis.input.Task; import mimis.input.Task;
import mimis.worker.Component; import mimis.worker.Worker;
import mimis.worker.Worker;
public class GlobalRouter extends Router {
public class GlobalRouter extends Component { protected String ip;
protected String ip; protected int port;
protected int port; protected Client client;
protected Client client;
public GlobalRouter(String ip, int port) {
public GlobalRouter(String ip, int port) { this.ip = ip;
this.ip = ip; this.port = port;
this.port = port; }
}
protected void activate() throws ActivateException {
protected void activate() throws ActivateException { try {
try { client = new Client(ip, port);
client = new Client(ip, port); } catch (IOException e) {
} catch (IOException e) { log.error(e);
log.error(e); throw new ActivateException();
throw new ActivateException(); }
} super.activate();
super.activate(); }
}
protected void deactivate() throws DeactivateException {
protected void deactivate() throws DeactivateException { super.deactivate();
super.deactivate(); client.stop();
client.stop(); }
}
public void task(Task task) {
public void task(Task task) { try {
try { client.send(task);
client.send(task); } catch (IOException e) {
} catch (IOException e) { log.error(e);
log.error(e); }
} }
}
class Client extends Worker {
class Client extends Worker { protected Socket socket;
protected Socket socket; protected ObjectInputStream objectInputStream;
protected ObjectInputStream objectInputStream; protected ObjectOutputStream objectOutputStream;
protected ObjectOutputStream objectOutputStream;
public Client(String ip, int port) throws IOException {
public Client(String ip, int port) throws IOException { socket = new Socket(ip, port);
socket = new Socket(ip, port); objectInputStream = new ObjectInputStream(socket.getInputStream());
objectInputStream = new ObjectInputStream(socket.getInputStream()); objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream = new ObjectOutputStream(socket.getOutputStream()); }
}
public void work() {
public void work() { try {
try { Object object;
Object object; do {
do { object = objectInputStream.readObject();
object = objectInputStream.readObject(); if (object instanceof Feedback) {
if (object instanceof Feedback) { add((Feedback) object);
add((Feedback) object); }
} } while (object != null);
} while (object != null); } catch (IOException e) {
} catch (IOException e) { log.error(e);
log.error(e); } catch (ClassNotFoundException e) {
} catch (ClassNotFoundException e) { log.error(e);
log.error(e); }
} }
}
protected void deactivate() throws DeactivateException {
protected void deactivate() throws DeactivateException { super.deactivate();
super.deactivate(); try {
try { objectInputStream.close();
objectInputStream.close(); objectOutputStream.close();
objectOutputStream.close(); socket.close();
socket.close(); } catch (IOException e) {
} catch (IOException e) { log.error(e);
log.error(e); }
} }
}
public void send(Object object) throws IOException {
public void send(Object object) throws IOException { objectOutputStream.writeObject(object);
objectOutputStream.writeObject(object); }
} }
} }
}

View File

@@ -0,0 +1,81 @@
package mimis.worker;
import java.util.Timer;
import java.util.TimerTask;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
public class IntervalWorker extends Worker {
protected static final boolean THREAD = true;
protected static final int INTERVAL = 500;
protected Timer timer;
public synchronized void start(boolean thread) {
if (!active) {
activate = true;
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
IntervalWorker.this.run();
}}, 0, INTERVAL);
active = true;
}
if (!thread) {
try {
synchronized (this) {
wait();
}
} catch (InterruptedException e) {
log.info(e);
}
}
}
public synchronized void stop() {
if (active) {
timer.cancel();
deactivate = true;
run();
notifyAll();
}
}
public void run() {
if (activate && !active) {
try {
super.activate();
} catch (ActivateException e) {
log.error(e);
} finally {
activate = false;
}
} else if (deactivate && active) {
try {
super.deactivate();
} catch (DeactivateException e) {
log.error(e);
} finally {
deactivate = false;
}
}
if (active) {
work();
}
}
protected void work() {
System.out.println("(-:");
}
public static void main(String[] args) {
IntervalWorker intervalWorker = new IntervalWorker();
for (int i = 0; i < 3; ++i) {
intervalWorker.start(false);
System.out.println("--");
intervalWorker.sleep(200);
intervalWorker.stop();
}
}
}

View File

@@ -1,122 +1,123 @@
package mimis.worker; package mimis.worker;
import mimis.exception.worker.ActivateException; import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException; import mimis.exception.worker.DeactivateException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
public abstract class Worker implements Runnable { public abstract class Worker implements Runnable {
protected Log log = LogFactory.getLog(getClass()); protected Log log = LogFactory.getLog(getClass());
protected static final boolean THREAD = false; protected static final boolean THREAD = true;
protected static final int SLEEP = 100; protected static final int SLEEP = 100;
protected boolean thread = true; protected boolean thread = true;
protected boolean interrupt; protected boolean run = false;
protected boolean run = false; protected boolean active = false;
protected boolean active = false; protected boolean activate = false;
protected boolean activate = false; protected boolean deactivate = false;
protected boolean deactivate = false;
public Worker(boolean thread) {
public Worker() {} this.thread = thread;
}
public Worker(boolean thread) {
this.thread = thread; public Worker() {
} this(THREAD);
}
public synchronized final void start(boolean thread) {
if (!active) { public synchronized void start(boolean thread) {
activate = true; if (!active) {
} activate = true;
if (!run) { }
run = true; if (!run) {
if (thread) { run = true;
log.debug("Start thread"); if (thread) {
new Thread(this, getClass().getName()).start(); log.debug("Start thread");
} else { new Thread(this, getClass().getName()).start();
log.debug("Run directly"); } else {
run(); log.debug("Run directly");
} run();
} else { }
notifyAll(); } else {
} notifyAll();
} }
}
public synchronized final void start() {
start(thread); public synchronized void start() {
} start(thread);
}
public synchronized final void stop() {
if (active) { public synchronized void stop() {
deactivate = true; if (active) {
} deactivate = true;
notifyAll(); }
} notifyAll();
}
public void exit() {
stop(); public void exit() {
run = false; stop();
} run = false;
}
protected void sleep(int time) {
try { protected void sleep(int time) {
if (time > 0) { try {
Thread.sleep(time); if (time > 0) {
} Thread.sleep(time);
} catch (InterruptedException e) { }
log.info(e); } catch (InterruptedException e) {
} log.info(e);
} }
}
protected void sleep() {
sleep(SLEEP); protected void sleep() {
} sleep(SLEEP);
}
public boolean active() {
return active; public boolean active() {
} return active;
}
protected void activate() throws ActivateException {
active = true; protected void activate() throws ActivateException {
} active = true;
}
protected void deactivate() throws DeactivateException {
active = false; protected void deactivate() throws DeactivateException {
} active = false;
}
public final void run() {
while (run || deactivate) { public void run() {
if (activate && !active) { while (run || deactivate) {
try { if (activate && !active) {
activate(); try {
} catch (ActivateException e) { activate();
log.error(e); } catch (ActivateException e) {
} finally { log.error(e);
activate = false; } finally {
} activate = false;
} else if (deactivate && active) { }
try { } else if (deactivate && active) {
deactivate(); try {
} catch (DeactivateException e) { deactivate();
log.error(e); } catch (DeactivateException e) {
} finally { log.error(e);
deactivate = false; } finally {
} deactivate = false;
} }
if (active) { }
work(); if (active) {
} else if (run) { work();
try { } else if (run) {
synchronized (this) { try {
wait(); synchronized (this) {
} wait();
} catch (InterruptedException e) { }
log.info(e); } catch (InterruptedException e) {
} log.info(e);
} }
} }
} }
}
protected abstract void work();
} protected abstract void work();
}

View File

@@ -0,0 +1,27 @@
package test;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;
public class CustomAppender extends AppenderSkeleton {
protected int bla = 9999;
public void setBla(int bla) {
this.bla = bla;
}
public int getBla() {
return bla;
}
public boolean requiresLayout() {
return true;
}
public void close() {}
protected void append(LoggingEvent loggingEvent) {
System.out.print(layout.format(loggingEvent));
}
}

BIN
java/wiiusej.jar Normal file

Binary file not shown.