zaterdag 30 oktober 2010

Calling Java from Javascript and calling Javascript from Java




This little sample shows that we can call javascript doAlert from Java.
Also we show that calling the function display will be received in Java with
data from Javascript.


applet.java


import java.applet.*;
import java.net.*;


public class applet extends Applet{
public void init(){
Display("start of java");
}
public void Display(String txt){
try{
getAppletContext().showDocument(new URL("javascript:doAlert(\""+txt+"\")"));
}catch(Exception e){}
}
}

index.html



<HTML>
<SCRIPT>
function doAlert(s) {
   alert(s);
}
</SCRIPT>
<APPLET CODE="applet.class" NAME="applet"  MAYSCRIPT >
</APPLET>
<INPUT type="button" value="Java.Display" onClick="document.applet.Display('hi')">
</HTML>

zondag 24 oktober 2010

Ultimate Deployment Appliance 2.0 install and setup

Ultimate Deployment Appliance or UDA is an installation environment
with which you can install (virtual) machines for a range of operating
systems.

Download UDA 2.0 from http://www.ultimatedeployment.org/

In my case I used VMware Player 3.0.1 to run it on my local computer.
The appliance has all the stuff in it, but I used an NFS server to hold
the storage for the ISOs

Install Steps:

Fixed IP address: 192.168.2.19
DHCP  + PXE checked for the client installations
root password: password

The Web Login (http://192.168.2.19) is admin with password : password

Storage - New Storage
  NFS     192.168.2.250
  /Volumes/SAMSUNG/    <- 1.5 Terra Byte USB disk
Mount

You need to put ISOs for the Operating Systems you want to install
on the NFS storage in order for UDA to
use them. These ISOs do not have to be bootable.
In one case I had a network directory which hold all the data
from the DVD.
I created an ISO from it using CDBurner XP (dragging the files to the
profile and used File - compile iso )

Once you have the ISOs in place go to the OS menu

OS
Give a name to the OS (max 8 character)
Select type of install (Linux, Windows ..)
Type in the path of the ISO on the NFS server

For the client to get a menu of operating systems to
choose for there must be templates.

In my case I used a virtual machine to install Windows XP on.
VMware Player started and selected 'Create new Virtual Machine'
Name the VM and Select No DVD or Image to install from
When the Virtual Machine starts Press F12
The machine will now do a PXE boot and if your UDA is the
only one who gives out DHCP addresses it will react on this
installation request.
In case you have a network environment where there is another
DHCP server you have to isolate your environment from it.
In case of using Virtual Machines just unplug your network cable.
With Physical Machines you propably have to use a switch or hub.
In some cases it could work just fine because the first DHCP
server who responses is the one that will be used. But keep
in mind that it is not wise to have this kind of setup active
long so that other clients who rely on the other (corporate) DHCP server
are not effected!

Write down the MAC address of your client. It is the address that
is shown when it is looking for a DHCP server

Template
 Name: MyComp
 MAC: 00-0C-29-xx-xx-xx  (VMware Player MAC address)
 OS: winxp
Finish

Press F12 again on the client and it should be presented with a menu
to choose the Operating System.

The standard setup program should start. Basically that is all you need
to do.

Changing TEMPLATEs

Default passwords and other setup configuration that the template makes
can be found in this location on the machine that runs UDA.

Login as root to UDA (password in my case is password)
cd /var/public/www/templates
Here you can find the default templates that your templates rely on.
vi [templatefile]
The password can be found in section:
[GuiUnattended]
AdminPassword
From WI2K3 into ...

Manually mount ISO on UDA

In case you want to have another ISO mounted on the server
to access it from the client you can do this:

(login as root to UDA)
Make mount point:
mkdir /var/public/tftproot/windows5/myown
Mount the ISO
mount -o loop /var/public/smbmount/[samsung]/myown.iso /var/public/tftproot/windows5/myown

Now this can be used inside the Operating System (Windows for instance:)
net use k: \\192.168.2.19\reminst\windows5\myown /user:guest ""

zaterdag 23 oktober 2010

JGame : Dynamically change contents of the background

For my project I wanted to put something on the background which should
not be a sprite and using getBufferGraphics() the image would appear on top
of other sprites. 
In fact I wanted to change a predefined Image from the engine.


The following steps were made to make this happen. Maybe there
is a better method, but this worked for me..



Modify

edit : src-jre\jgame\platform\JREImage.java

public class  JREImage

instead of class JREImage

and make both of the constructor public as well:

public JREImage(....
public JREImage()

===================================

edit src-base\jgame\JGImage.java
On the top :

package ..
import java.awt.*;
(add to the bottom .. before last } <= which is from the class)
public Image getImage();
public void setImage(Image img);


======================================

Edit src-jre\jgame\platform\JREImage.java
(add to the bottom .. before last } <= which is from the class)

public Image getImage(){
return this.img;
}


public void setImage(Image img){
this.img=img;
}


======================================

Edit src-jre\jgame\platform\JGEngine.java

Add this before another defined defineImage function ..


public void defineImage(String imgname,  
JGImage jig){
el.defineImage(imgname,jig);
}




Edit src-base\jgame\impl\EngineLogic.java

Add this before another defineImage function ..

// replace definedImage with new image
public void defineImage(String name, JGImage img){
String imgfile=name;


if (images_loaded.containsKey(name)) {
imgfile=(String)images_loaded.get(name);
undefineImage(name);
}
defineImage(name,"p", 0, img,"-",0,0,0,0);
if(imgfile.length() <1)imgfile=name;
images_loaded.put(name,imgfile);


}





===

Some sample code (modify for example the other JGame code and put the action after a keypress)

public Image readImage(String imagestring){
                String inputLine;
Image retval=null;
                try{


                   URL Url = new URL(getCodeBase().toString()+imagestring);
   retval = ImageIO.read(Url);
                }catch(Exception readfile){
//System.out.println("Read File exception: "+readfile.toString());
}
return retval;


}


// Image load
Image a=readImage(theimage);


// This retrieves JGImage from the background defined image
JGImage bgimg=getImage("background");


// Our own routine to create an Image object from it
java.awt.Image img=bgimg.getImage();


// Make an image to draw stuff on
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
// always use bitmask transparency

BufferedImage buf = gc.createCompatibleImage(img.getWidth(this),img.getHeight(this),Transparency.BITMASK);
Graphics gg=buf.getGraphics();
gg.drawImage(img,0,0,this);






// on top of background draw our own thing
gg.drawImage(a, imagex, imagey, this);


setBGImage("background");


// Make our own JREImage
JREImage je=new JREImage((Image)buf);
// Call our own routine to replace the defined image with the composed image
defineImage("background",(JGImage)je);

// Set the new background
setBGImage("background");



==================================================================



To recompile the jar file:

make-base

Copy the JAR file to your project directory..

dinsdag 19 oktober 2010

Dynamic HTML page using PHP and DOM

For a project I wanted to make a property system that
could connect to MySQL to retrieve and set properties
which would be entered on a HTML page.

Interaction between form elements and php scripts that
eventually would connect to a database to get and set
all the data.

Mouse coordinates are shown on the board, but the next
step which would involve setting the property isn't
implemented.




frames.php

<?php


echo <<<HTML


<iframe name="editor" id="editor" scrolling="auto" width=800 height=600 
src="edit.php?cmd=board" style = "position:absolute;left:1px;top:1px">
</iframe>


<iframe name="properties" id="properties" scrolling="auto"  marginheight="14" 
src="edit.php?cmd=properties" style = "position:relative;left:800px;top:1px;">
</iframe>


<iframe name="data" id="data" scrolling="auto"  src="edit.php?cmd=data"
 style = "position:relative;left:900px;top:1px;">
</iframe>


HTML;
?>


board.php



<?php


echo <<<HTML
<head>
<link rel="stylesheet" href="css/mycss.css">
</head>
<form name="Show">
<div class="game_area" width=800px height=600px>
</div>
</form>
<script>
var IE = document.all?true:false;
if (!IE){
         document.captureEvents(Event.MOUSEMOVE)
         document.captureEvents(Event.MOUSECLICK)
}
document.onmousemove = getMouseXY;
document.onmousedown = getMouseClick;
var tempX = 0;
var tempY = 0;
function getMouseXY(e) {
        if (IE) {
                tempX = event.clientX + document.body.scrollLeft;
                tempY = event.clientY + document.body.scrollTop;
        }
        else {
                tempX = e.pageX;
                tempY = e.pageY;
        }
        if (tempX < 0){tempX = 0;}
        if (tempY < 0){tempY = 0;}
if(IE){
parent.document.getElementById('properties').contentWindow.document.getElementById('x').value=tempX;
parent.document.getElementById('properties').contentWindow.document.getElementById('y').value=tempY;
}else{
parent.document.getElementById('properties').contentDocument.getElementById('x').value=tempX;
parent.document.getElementById('properties').contentDocument.getElementById('y').value=tempY;
}
return true;
}
function getMouseClick(e){
        if (IE) {
                if(event.button > 0)
parent.document.getElementById('properties').contentWindow.document.getElementById('button').value=event.button;
        }else{
                if(event.which > 0)
parent.document.getElementById('properties').contentDocument.getElementById('button').value=event.which;
        }
return true;
}
</script>
HTML;


?>



mycss.css


.game_area {
        background-color: #08000d;
        width: 800px;
        height: 600px;


}









properties.php

The database button COULD run a PHP script that uses a MySQL database.
<?php
        echo <<<HTML
        <FORM name=properties>
                X <input type=text id="x" name="x"><br>
                Y <input type=text id="y" name="y"><br>
                B <input type=text id="button"name="button"><br>
                <input value="database" type=button 
onclick="javascript:parent.document.getElementById('data').src='edit.php?cmd=data';"><br>
        </FORM>
HTML;
?>


data.php

But we do not connect to a database for this demonstration. We just retrieve
the x coordinate and show it.

<?php
        echo <<<HTML


        <script>
                function onload(){
                        var IE = document.all?true:false;
HTML;




        echo "if (!IE){";
                // chrome:
                echo "alert(' Not Internet Explorer '+parent.document.getElementById('properties').
contentDocument.getElementById('x').value+'');";
        echo "}else{";
                // IE
                echo "alert(' Internet Explorer  '+parent.document.getElementById('properties').
contentWindow.document.getElementById('x').value+'');";
        echo "}";


        // Complete function
        echo <<<HTML
                        return true;
                }
        </script>
        <body onload='onload();'>
        </body>
HTML;


?>

donderdag 7 oktober 2010

Virtual Machine CentOS 5.4 running PHP, MYSQL Apache (LAMP)

Hardware: Mac Mini (192.168.2.250)
VMWare Fusion: 3.0.1            

CentOS 5.4 Virtual Machine installed with Server packages

The network router forwards traffic from port 80 to 192.168.2.80

// Check
vi /etc/sysconfig/network-scripts/ifcfg-eth0
IPADDR=192.168.2.80
GATEWAY=192.168.2.254
NETMASK=255.255.255.0


Root access permit in /etc/ssh/sshd_config
PermitRootLogin yes

install php:
yum install php


install mysql-server mysql:
yum install mysql-server mysql


yum install php-mysql

// start apache

service httpd restart

// Every time after reboot
chkconfig httpd on
chkconfig mysqld on

// Mysql password
mysqladmin -u root password hiddenpass

 
index.php
<?php

$db=mysql_connect('localhost', 'root', 'hiddenpass');
mysql_select_db("test", $db);
$result=mysql_query("SELECT * from MARTIN");
if($r=mysql_fetch_assoc($result)){
echo "a : {$r['a']}\n";
}

Resume:
- Apache 2
- PHP 5.1.6
- MySQL 14.12

Logical Volume Manager and one big volume with multiple disks


How to create one big LVM with multiple disks

NFS Storage

REMEMBER: If you want to create LVM on the rootdisk you
should install LVM from scratch. You cannot convert the
bootdisk without creating a temporary disk. You can
resize the partition but there must be more than half of
free space and you need to boot into single user mode!!!!
The storage that we added to a Fedora 8 machine is
attached to the SCSI bus. We put a SCSI controller
in the machine and attached some SCSI storage to
the machine.
We want to make this storage accessible through the NFS
environment so that VMware and Unix machines can access it
Mainly for test purposes so the storage is Read Write for all!
The disks:
/dev/sdb, /dev/sdc, /dev/sdd are 18 GB disks
/dev/sde, /dev/sdf are 34 GB disks

FDISK actions

The disks were used by other OSes so first I deleted the partition
using fdisk
storage# fdisk /dev/sdb

The number of cylinders for this disk is set to 34732.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
1
d = delete partition
Selected partition 1
w = write
q = quit
 
Repeat this for all disks
 

Logical Volume Manager actions

storage# pvcreate /dev/sdb

Repeat this for all disks
We create a volume group called vg storage# vgcreate vg /dev/sdb You can add all the devices to this command :
vgcreate vg /dev/sdb /dev/sdc /dev/sdd ...
but I will show you how to extend
With this method you can always add disks to the
volume group
storage# lvcreate vg --size 18G

Logical volume "lvol0' created The size may vary due to the block size and administration
that LVM puts on the disk
So it could be that you can only allocate 17 or 16 GB
Check the device path, the /dev/vg is the volume group and
the lvol0 is the logical volume : 
storage# ls /dev/vg/lvol0
Make a file system on it: 
storage# mkfs -t ext3 -v /dev/vg/lvol0
Mount it: 
storage# mkdir -p /mnt/storage

storage# mount /dev/vg/lvol0 /mnt/storage
storage# touch /mnt/storage/hello
storage# ls /mnt/storage/*
hello lost+found
Now extend the volume group with an extra disk
 storage# vgextend vg /dev/sdc

storage# lvextend /dev/vg/lvol0 --size 36G 
The lvextend command needs the storage amount after
the extend. So if the first was 18 GB and we add another 18 GB
it will become 36 ... etc.. for all the rest of the disks
The filesystem needs to grow with it: (or shrink in the case reduction)
 storage# resize2fs /dev/vg/lvol0

Check the size of the disk:
 storage# df -k

Add the export to NFS

storage# vi /etc/exports

/mnt/storage    *(rw,sync,no_root_squash,insecure)
And run the nfs server:
 storage# /etc/init.d/nfs restart