how to run process

Here is the way to wrap external console-executable stuff.

private Process Process = null;
String command = "ping google.com"; 
Runtime r = Runtime.getRuntime();
		Process = r.exec(command);
		BufferedInputStream = new BufferedInputStream(Process.getInputStream());// bytes from the process
		BufferedOutputStream = new BufferedOutputStream(Process	.getOutputStream());// bytes to the process
...
                Process.destroy();
		System.out.println("------------------> Process killed.");

how to create window (JFrame)

package triplane.gui;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
 
import javax.swing.JFrame;
 
public class Window extends JFrame {
	int h = 768;
	int w = 1024;
	Image Background;
	InputInterceptor Inter;
 
	public Window(String Title, int H, int W, InputInterceptor Inter) {
		this.Inter = Inter;
		this.Background = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
		// test();
 
		setTitle(Title);
		setMinimumSize(new Dimension(W, H));
		setMaximumSize(new Dimension(W, H));
		setResizable(false);
		JFrame.setDefaultLookAndFeelDecorated(true);
 
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int h = screenSize.height;
		int w = screenSize.width;
 
		setLocation((w - W) / 2, (h - H) / 2);
 
		// 2. Optional: What happens when the frame closes?
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
		addWindowListener(this.Inter);
		addKeyListener(this.Inter);
		addMouseListener(this.Inter);
 
		// 3. Create components and put them in the frame.
		// ...create emptyLabel...
		// frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
		getContentPane();
 
		// 4. Size the frame.
		pack();
 
		// 5. Show it.
		setVisible(true);
 
	}
 
	private void test() {
		// TODO Auto-generated method stub
		// BufferedImage im = new BufferedImage(600, 400,
		// BufferedImage.TYPE_INT_RGB);
		// Graphics g = im.getGraphics();
		// g.setColor(Color.RED);
		// g.drawLine(0, 0, 100, 100);
		// I.put(im);
	}
 
	public void paint(Graphics g) {
		this.setBackground(Color.BLACK);
		g.drawImage(this.Background, 0, 0, this);
		// if (I != null) {
		// if (I.check_availability()) {
		// Image i = I.get();
		//
		// g.drawImage(i, (this.getWidth() - i.getWidth(this)) / 2, (this
		// .getHeight() - i.getHeight(this)) / 2, this);
		// }
		// }
	}
 
	public void update(Graphics g) {
		paint(g);
	}
 
}
package triplane.gui;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
 
public class InputInterceptor implements KeyListener, ActionListener,
		MouseListener, WindowListener {
 
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
 
		System.out.println(e);
 
	}
 
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
	}
 
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
	}
 
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
	}
 
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
	}
 
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
	}
 
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
	}
 
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
	}
 
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
	}
 
	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
	}
 
	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
	}
 
	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
	}
 
	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
	}
 
	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
	}
 
	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
	}
 
	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
	}
 
}

see

Base64

code fileData string
MiGBase64 Base64.java
http://migbase64.sourceforge.net/

Java Architecture for XML Binding (JAXB)

http://java.sun.com/developer/technicalArticles/WebServices/jaxb/

http://java.sun.com/developer/technicalArticles/WebServices/jaxb/xml_schema_fig1.gif

package rebecca.e.remote;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
 
public class XMLMarshal {
 
	public Object UnMurshall(String xml, String packageName)
			throws JAXBException {
		JAXBContext jc = JAXBContext.newInstance(packageName);
		Unmarshaller u = jc.createUnmarshaller();
		InputStream is = new ByteArrayInputStream(xml.getBytes());
		return u.unmarshal(is);
	}
 
	public String Murshall(Object xmlObject, String packageName)
			throws JAXBException {
 
		JAXBContext jc = JAXBContext.newInstance(packageName);
		Marshaller m = jc.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		ByteArrayOutputStream is = new ByteArrayOutputStream();
		m.marshal(xmlObject, is);
		return is.toString();
	}
 
}

java files

package rebecca.e.util.io;
 
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
 
public class FilePacker {
            /// Save FileData from memory to File
	public void String2File(String f, String data, boolean b)
			throws IOException {
		// TODO Auto-generated method stub
		if (b)
			System.out.println("Writing: " + f);
		FileOutputStream fos = new FileOutputStream(f);
		fos.write(data.getBytes());
		fos.flush();
		fos.close();
 
	}
 
	void printfile(String p, File f) {
		System.out.println(p + f.getName() + ":" + f.getData());
		// this.Attributes.get(i)
	}
 
	void printfolder(String p, Dir d) {
		print0(p, d);
	}
            /// Print loaded folder tree
	public void print(Dir d) {
		this.print0(" ", d);
		// TODO Auto-generated method stub
 
	}
            /// Print loaded file
	public void print(File f) {
		this.printfile(" ", f);
		// TODO Auto-generated method stub
 
	}
 
	void print0(String p, Dir d) {
		System.out.println(p + " ");
		for (int i = 0; i < d.getFolder().size(); i++) {
			this.printfolder(p + "   ", d.getFolder().get(i));
 
		}
 
		for (int i = 0; i < d.getFile().size(); i++) {
			this.printfile(p + " ", d.getFile().get(i));
		}
 
		// // TODO Auto-generated method stub
		// String t0 = p + t + "\n";
		// t0 = t0 + p + "";
		// for (int i = 0; i < this.Attributes.size(); i++) {
		// t0 = t0 + p + Attributes.getKey(i) + ":" + this.Attributes.get(i)
		// + "\n";
		//
		// }
		// for (int i = 0; i < this.SubNodes.size(); i++) {
		// print0(" " + p, SubNodes.get(i).toString());
		//
		// }
		//
		// return t0;
	}
            /// Load file to memory (String)
	public String File2String(String f, boolean b) throws IOException {
		// TODO Auto-generated method stub
		FileInputStream fstream;
		DataInputStream in;
		int bb;
		fstream = new FileInputStream(f);
		in = new DataInputStream(fstream);
 
		ByteArrayOutputStream out = new ByteArrayOutputStream();
 
		if (b) {
			System.out.println("Reading: " + f);
		}
		while ((bb = in.read()) != -1) {
 
			out.write(bb);
 
		}
		String s = out.toString();
 
		out.close();// -Djava.security.policy=C:/rmi.plc
		in.close();
		fstream.close();
 
		return s;
	}
            /// Platform-independent File (Dir) separator
	public static String iV = System.getProperty("file.separator");
 
           ///Archiving
	public void addFile2Dir(Dir D, String filePath, boolean b)
			throws IOException {
		java.io.File f = new java.io.File(filePath);
		if (f.isFile()) {
			File F = new File();
			F.setName(f.getName());
			F.setData(this.File2String(filePath, true));
			// F.setData("some data");
			D.getFile().add(F);
		}
		if (f.isDirectory()) {
			Dir Dr = new Dir();
			Dr.setName(f.getName());
 
			String[] l = f.list();
 
			for (int i = 0; i < l.length; i++) {
				this.addFile2Dir(Dr, filePath + iV + l[i], b);
			}
			D.getFolder().add(Dr);
		}
	}
           ///Extracting
	public void ExtractToFolder(Dir d, String Dir, boolean b)
			throws IOException {
 
		mkDir(Dir, b);
 
		List l = d.getFile();
		for (int i = 0; i < l.size(); i++) {
			File f = l.get(i);
			String name = f.getName();
			String data = f.getData();
 
			String2File(Dir + iV + name, data, b);
 
		}
 
		List j = d.getFolder();
		for (int i = 0; i < j.size(); i++) {
			Dir f = j.get(i);
			String name = f.getName();
			mkDir(Dir + iV + name, b);
			ExtractToFolder(f, Dir + iV + name, b);
			// String2File(Dir + iV + name, data, b);
 
		}
 
	}
 
	private void mkDir(String Dir, boolean b) {
		// TODO Auto-generated method stub
 
		if (b)
			System.out.println("Creating: " + Dir);
 
		if (!new java.io.File(Dir).mkdir()) {
			if (b)
				System.out.println("Unable to create folder: " + Dir + " "
						+ "(Already exist?)");
 
		}
 
	}
 
}

How to: RMI

Steps to creation of an RMI system:

The short version
1) Create an interface. (in this case, the interface is myRMIInterface.java).
2) Create a class that implements the interface. (in this case, myRMIImpl.java).
3) Create a server that creates an instance of this class
4) Create a client that connects to the server object using Naming.lookup()
5) Compile these classes.
6) Run the RMI interface compiler on the .class file of the implementation
class (in this case, you’d say “rmic myRMIImpl”).
7) Start the RMI registry (on Windows NT/95, say “start rmiregistry”).
8) Start the server class (“start java myRMIServer”).
9) Run the client program (“java myRMIClient”).

The long version: http://patriot.net/~tvalesky/easyrmi.html

read from console java example

import java.util.Scanner;
 
public class InputExp {
 
   public static void main(String[] args) {
 
       String name;
       int age;
       Scanner in = new Scanner(System.in);
 
       // Reads a single line from the console 
       // and stores into name variable
       name = in.nextLine();
 
       // Reads a integer from the console
       // and stores into age variable
       age=in.nextInt();
       in.close();            
 
       // Prints name and age to the console
       System.out.println("Name :"+name);
       System.out.println("Age :"+age);
 
    }
}

HollyShitException

package rebecca.e.util;
 
public class HollyShitException extends RuntimeException {
 
	private static final long serialVersionUID = 1811988306094364489L;
 
	public HollyShitException(String Message) {
		super("Holly shit! \n" + Message);
	}
 
}

ReentrantLock

http://en.wikipedia.org/wiki/Dining_philosophers_problem

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/ReentrantLock.html

A reentrant mutual exclusion Lock with the same basic
behavior and semantics as the implicit monitor lock accessed using
synchronized methods and statements, but with extended
capabilities.

A ReentrantLock is owned by the thread last
successfully locking, but not yet unlocking it. A thread invoking
lock will return, successfully acquiring the lock, when
the lock is not owned by another thread. The method will return
immediately if the current thread already owns the lock. This can
be checked using methods isHeldByCurrentThread(), and getHoldCount().

The constructor for this class accepts an optional
fairness parameter. When set true, under
contention, locks favor granting access to the longest-waiting
thread. Otherwise this lock does not guarantee any particular
access order. Programs using fair locks accessed by many threads
may display lower overall throughput (i.e., are slower; often much
slower) than those using the default setting, but have smaller
variances in times to obtain locks and guarantee lack of
starvation. Note however, that fairness of locks does not guarantee
fairness of thread scheduling. Thus, one of many threads using a
fair lock may obtain it multiple times in succession while other
active threads are not progressing and not currently holding the
lock.
Also note that the untimed tryLock method does not
honor the fairness setting. It will succeed if the lock
is available even if other threads are waiting.

It is recommended practice to always immediately
follow a call to lock with a try block, most
typically in a before/after construction such as:

 class X {
   private final ReentrantLock lock = new ReentrantLock();
   // ...

   public void m() {
     lock.lock();  // block until condition holds
     try {
       // ... method body
     } finally {
       lock.unlock()
     }
   }
 }

UniMod

http://unimod.sourceforge.net/

UniMod states for Unified Modeling. Long term project goal is to create unified methodology for application development process that will close the gap between Design and Development phases.

Currently, UniMod project is focused on designing and implementing applications behavior. Already implemented approach adapts SWITCH-technology for UML notation. SWITCH-technology is also known as Automata-based Programming and has it’s own Russian site http://is.ifmo.ru/english/.

SWITCH-technology suggests to model application behavior with a help of Structural Finite State Machine (FSM). Structural FSM is defined as set of Abstract FSMs and scheme of Abstract FSMs interconnections.

Eclipse plugin:
http://unimod.sourceforge.net/eclipse-plugin.html

←Older