Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database Access from Applet security ??

Status
Not open for further replies.

cohans

Programmer
Aug 21, 2001
35
US
We are trying to read some data from MySQL database from an applet. The output is a Graphic shown in the form of network connecting nodes. We are using an open API from TomSawyer Software, a graphic layout tool.
As a standalone application, the program runs absolutely fine. But after converting to an applet, we get an "access denied" error when it tries to load.(java.util.PropertyPermission java.class.path read)

Any suggestions??? Thanks.

I am pasting the source code from the HTML file and the java source code.

<html>
<head>
<title>step 5</title>
</head>

<body>


<OBJECT classid=&quot;clsid:8AD9C840-044E-11D1-B3E9-00805F499D93&quot;
WIDTH = 600 HEIGHT = 500 codebase=&quot;<PARAM NAME = CODE VALUE = &quot;BSAApplet1.class&quot; >
<PARAM NAME = ARCHIVE VALUE = &quot;ts4.jar&quot; >

<PARAM NAME=&quot;type&quot; VALUE=&quot;application/x-java-applet;version=1.3&quot;>

<PARAM NAME=&quot;scriptable&quot; VALUE=&quot;false&quot;>
<COMMENT>
<EMBED type=&quot;application/x-java-applet;version=1.3&quot; CODE = &quot;BSAApplet1.class&quot; WIDTH = 600 HEIGHT = 500 scriptable=false pluginspage=&quot;
</NOEMBED></EMBED>
</OBJECT>

<!--
<APPLET CODE = &quot;BSAApplet1.class&quot; ARCHIVE = &quot;ts4.jar&quot; WIDTH = 600 HEIGHT = 500>


</APPLET>
-->



</body>
</html>




Java Source

import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import com.tomsawyer.editor.*;
import com.tomsawyer.editor.state.*;
import com.tomsawyer.editor.ui.*;
import com.tomsawyer.editorx.ui.*;
import com.tomsawyer.glt.*;
//import com.tomsawyer.graph.*;

//import ui.*;
import java.util.*;

// database support
import gov.uspto.sdi.common.*;
//import org.gjt.mm.mysql.*;

//
// This class contains the example editor.
//

public class BSAApplet extends JApplet
implements ActionListener {
// common components
private PropsFile pfDB = new PropsFile( &quot;db.properties&quot; );
// private LogFile lf = new LogFile( &quot;example1.log&quot; );
java.sql.Connection connection;

//1 private TSEGraph graph;
private TSDigraph digraph;
private TSEGraph graph;
private TSEGraphWindow graphWindow;
private ArrayList nodes = new ArrayList();
private ArrayList edges = new ArrayList();

// These constants hold strings which represent commands.
public static final String CREATE_SERVER = &quot;Create Server&quot;;
public static final String CREATE_CLIENT = &quot;Create Client&quot;;
public static final String CREATE_EDGES = &quot;Create Edges&quot;;
public static final String SELECT = &quot;Select&quot;;
public static final String DELETE = &quot;Delete&quot;;


public BSAApplet() {
}

public BSAApplet( ArrayList nodes, ArrayList edges ) {
this.nodes = nodes;
this.edges = edges;
}

public void init() {
}

public void start() {
// Initialize the graph.
this.initGraph();

// Initialize the GUI.
this.initGUI();

// Initialize the graph window. Set the graph and default
// state.
this.graphWindow.setDefaultState(new TSESelectState());
this.graphWindow.switchState(this.graphWindow.getDefaultState());
}

public void stop() {}
public void destroy() {}

public void initGraph() {
TSManager.initGLT();

String[] node, edge;
TSENode[] graphNodes = new TSENode[ nodes.size() ];
TSNode[] digraphNodes = new TSNode[ nodes.size() ];
ArrayList diedges = new ArrayList();
digraph = new TSDigraph();

// create a dinode for every node
Iterator e0 = nodes.iterator();
for( int i = 0; e0.hasNext(); i++ ) {
e0.next();
digraphNodes = (TSNode) digraph.addNode();
}

// create a diedge for each edge
Iterator e1 = edges.iterator();
while( e1.hasNext() ) {
edge = (String[]) e1.next();
digraph.addEdge( digraphNodes[ getNodeIndex( edge[1] ) ], digraphNodes[ getNodeIndex( edge[2] ) ] );
}

// set the layout style
//digraph.layoutStyle(TSLayoutStyle.HIERARCHICAL);
//digraph.layoutStyle(TSLayoutStyle.SYMMETRIC);
//digraph.layoutStyle(TSLayoutStyle.ORTHOGONAL);
digraph.layoutStyle(TSLayoutStyle.CIRCULAR);

// perform the layout
digraph.layout();

// build the real graph with the coordinates supplied by
// the layout digraph
this.graph = new TSEGraph();
Iterator e2 = nodes.iterator();
//BSANodeLabelUI ui;
//TSEImageNodeUI ui = new TSEImageNodeUI();
//ui.setImage(new TSEImage(TSEImageNodeUI.class,
//&quot;images/cb5.gif&quot;));
TSEImageNodeUI ui = new TSEImageNodeUI();
ui.setImage(new TSEImage(this.getClass(),&quot;images/cb0.gif&quot;));
String imageSuffix = &quot;&quot;;

for( int i = 0; e2.hasNext(); i++ ) {
// extract the companion node and dinode
String[] n = (String[]) e2.next();
TSNode n0 = digraphNodes;

// add the node to the graph
//ui = new BSANodeLabelUI( n[1] );
graphNodes = (TSENode) this.graph.addNode();
// set image
double rand = Math.random();

if( rand < 0.1 ) {
imageSuffix = &quot;blu&quot;;
} else if( rand < 0.2 ) {
imageSuffix = &quot;yel&quot;;
} else if( rand > 0.9 ) {
imageSuffix = &quot;red&quot;;
} else if( rand > 0.8 ) {
imageSuffix = &quot;grn&quot;;
} else {
imageSuffix = &quot;gry&quot;;
}

ui.setImage(new TSEImage(this.getClass(),&quot;images/cb_&quot;
+ imageSuffix + &quot;.gif&quot;));
//( (TSENode) graphNodes ).setUI( ui );
( (TSENode) graphNodes ).setUI((TSEObjectUI) ui.clone());
( ( (TSENode) graphNodes ).addLabel() ).setTag(n[1]);
//System.out.println( ( ( ( (TSENode) graphNodes ).addLabel() ).getSize() ).toString() );
// set the coordinates
//( (TSENode) graphNodes ).name = n;
( (TSENode) graphNodes ).setCenter( n0.x(), n0.y() );
}

Iterator e3 = edges.iterator();
for( int i = 0; e3.hasNext(); i++ ) {
edge = (String[]) e3.next();
this.graph.addEdge( graphNodes[ getNodeIndex( edge[1] ) ], graphNodes[ getNodeIndex( edge[2] ) ] );
}

digraph.discard();
digraph = null;
}

public void initGUI() {
// Set up the main frame window.
JFrame rootFrame = new JFrame();

rootFrame.getContentPane().setLayout(new BorderLayout());

// Create the graph window, which is the main component of the
// example editor.
this.graphWindow = new TSEGraphWindow(this.graph);
this.graphWindow.setZoomLevel( 1, true );

// Create a button panel.
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 1));

// Create a server button and add it to the button panel.
JButton serverButton = new JButton(&quot;Server&quot;);
serverButton.setActionCommand(this.CREATE_SERVER);
serverButton.addActionListener(this);
buttonPanel.add(serverButton);

// Create a client button and add it to the button panel.
JButton clientButton = new JButton(&quot;Client&quot;);
clientButton.setActionCommand(this.CREATE_CLIENT);
clientButton.addActionListener(this);
buttonPanel.add(clientButton);

// Create a connect button and add it to the button panel.
JButton connectButton = new JButton(&quot;Connect&quot;);
connectButton.setActionCommand(this.CREATE_EDGES);
connectButton.addActionListener(this);
buttonPanel.add(connectButton);

// Create a select button and add it to the button panel.
JButton selectButton = new JButton(&quot;Select&quot;);
selectButton.setActionCommand(this.SELECT);
selectButton.addActionListener(this);
buttonPanel.add(selectButton);

// Add the two components, the button panel, and the
// graph window.
rootFrame.getContentPane().add(this.graphWindow,
BorderLayout.CENTER);
//rootFrame.getContentPane().add(buttonPanel,
//BorderLayout.EAST);

// Set a default size and make the application visible.
rootFrame.setSize(400, 400);
rootFrame.setVisible(true);

// Add a keyboard binding to the graph window.
this.graphWindow.registerKeyboardAction(this,
this.DELETE,
KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);

// Center the graph in the graph window.
this.graphWindow.centerGraph(true);

// Make sure that the application knows when the user
// wishes to close it.
//rootFrame.addWindowListener(this);
}

public void actionPerformed(ActionEvent event) {
String action = event.getActionCommand();

if (action == this.CREATE_SERVER) {
// The server button is pressed so set the corresponding node UI.
TSEImageNodeUI ui = new TSEImageNodeUI();
//ui.setImage(new TSEImage(TSEImageNodeUI.class, &quot;images/server.gif&quot;));
//this.graphWindow.setCurrentNodeUI(ui);
// Switch to the create node state.
this.graphWindow.switchState(new TSECreateNodeState());

} else if (action == this.CREATE_CLIENT) {
// The client button is pressed so set the corresponding node UI.
//TSEImageNodeUI ui = new TSEImageNodeUI();
//ui.setImage(new TSEImage(TSEImageNodeUI.class, &quot;images/terminal.gif&quot;));
//this.graphWindow.setCurrentNodeUI(ui);
// Switch to the create node state.
this.graphWindow.switchState(new TSECreateNodeState());

} else if (action == this.CREATE_EDGES) {
this.graphWindow.switchState(new TSECreateEdgeState());

} else if (action == this.SELECT) {
this.graphWindow.switchState(new TSESelectState());

} else if (action == this.DELETE) {
// Delete the selected items.
this.graphWindow.deleteSelected();
}
}

public void setNodes( ArrayList nodes ) {
this.nodes = nodes;
}

public void setEdges( ArrayList edges ) {
this.edges = edges;
}

private int getNodeIndex( String code ) {
int index = 0;
String[] node;

Iterator e = nodes.iterator();
for( int i = 0; e.hasNext(); i++ ) {
node = (String[]) e.next();
if( node[1].equals( code ) ) {
return i;
}
}
return index;
}

public void getData() {
getData( &quot;&quot; );
}

public void getData( String where ) {
try {
// connect to the database
Class.forName(&quot;org.gjt.mm.mysql.Driver&quot;).newInstance();
String url = pfDB.getProp( &quot;bsa.url&quot; );

connection = DriverManager.getConnection( url );
System.out.println( &quot;connection established: &quot; + !connection.isClosed() );

// select the system data and pack the list
Statement statement1 = connection.createStatement();
String selectSystems =
&quot;select sys_key, sys_code, sys_name&quot;
+ &quot; from systems&quot;
+ ( where == &quot;&quot; ? &quot;&quot; : &quot; where &quot; + where )
+ &quot; order by sys_code&quot;;

ResultSet rs1 = statement1.executeQuery( selectSystems );
while( rs1.next() ) {
System.out.println(
rs1.getInt( &quot;sys_key&quot; )
+ &quot;, &quot; + rs1.getString( &quot;sys_code&quot; )
+ &quot;, &quot; + rs1.getString( &quot;sys_name&quot; )
);

String[] s = new String[3];
s[0] = new Integer( rs1.getInt( &quot;sys_key&quot; ) ).toString();
s[1] = rs1.getString( &quot;sys_code&quot; );
s[2] = rs1.getString( &quot;sys_name&quot; );
nodes.add( s );
}

// select the system_association data and pack the list
Statement statement2 = connection.createStatement();
String selectEdges =
&quot;select sa.sa_key, s1.sys_code, s2.sys_code&quot;
+ &quot; from system_associations sa, systems s1, systems s2&quot;
+ &quot; where sa.parent_sys_key = s1.sys_key&quot;
+ &quot; and sa.child_sys_key = s2.sys_key&quot;
+ ( where == &quot;&quot; ? &quot;&quot; : &quot; and &quot; + where )
+ &quot; order by s1.sys_code, s2.sys_code&quot;;

ResultSet rs2 = statement2.executeQuery( selectEdges );
while( rs2.next() ) {
System.out.println(
rs2.getInt( &quot;sa.sa_key&quot; )
+ &quot;, &quot; + rs2.getString( &quot;s1.sys_code&quot; )
+ &quot;, &quot; + rs2.getString( &quot;s2.sys_code&quot; )
);

String[] s = new String[3];
s[0] = new Integer( rs2.getInt( &quot;sa.sa_key&quot; ) ).toString();
s[1] = rs2.getString( &quot;s1.sys_code&quot; );
s[2] = rs2.getString( &quot;s2.sys_code&quot; );
edges.add( s );
}

// ready to go...
start();

} catch( Exception e ) {
e.printStackTrace( System.out );
} finally {
try {
connection.close();
} catch( SQLException e ) {
e.printStackTrace( System.out );
}
}

//setNodes( nodes );
//setEdges( edges );

}

public void test() {
String code = &quot;sys&quot;;
String name = &quot;system&quot;;

// build a list of nodes
for( int i = 0; i < 9; i++ ) {
String[] s = new String[3];
s[0] = new Integer(i).toString();
s[1] = code + i;
s[2] = name + i;

nodes.add( s );
}

// build a list of edges
String[] a = { &quot;1&quot;, &quot;sys0&quot;, &quot;sys2&quot; };
String[] b = { &quot;2&quot;, &quot;sys0&quot;, &quot;sys4&quot; };
String[] c = { &quot;3&quot;, &quot;sys0&quot;, &quot;sys6&quot; };
String[] d = { &quot;4&quot;, &quot;sys0&quot;, &quot;sys8&quot; };
String[] e = { &quot;13&quot;, &quot;sys0&quot;, &quot;sys5&quot; };
String[] f = { &quot;5&quot;, &quot;sys2&quot;, &quot;sys1&quot; };
String[] g = { &quot;6&quot;, &quot;sys2&quot;, &quot;sys3&quot; };
String[] h = { &quot;7&quot;, &quot;sys4&quot;, &quot;sys2&quot; };
String[] i = { &quot;8&quot;, &quot;sys4&quot;, &quot;sys3&quot; };
String[] j = { &quot;11&quot;, &quot;sys5&quot;, &quot;sys7&quot; };
String[] k = { &quot;9&quot;, &quot;sys6&quot;, &quot;sys5&quot; };
String[] l = { &quot;10&quot;, &quot;sys6&quot;, &quot;sys7&quot; };
String[] m = { &quot;12&quot;, &quot;sys8&quot;, &quot;sys6&quot; };
String[] n = { &quot;13&quot;, &quot;sys6&quot;, &quot;sys8&quot; };

edges.add( a );
edges.add( b );
edges.add( c );
edges.add( d );
edges.add( e );
edges.add( f );
edges.add( g );
edges.add( h );
edges.add( i );
edges.add( j );
edges.add( k );
edges.add( l );
edges.add( m );
edges.add( n );

setNodes( nodes );
setEdges( edges );

start();
}

public static void main(String[] args) {
// A final variable is used since it should not be garbage-
// collected after the main method is completed.
if( args.length != 1 ) {
System.out.println( &quot;\nUsage:\n&quot; );
System.out.println( &quot;\tjava BSAApplet test\n&quot; );
return;
}

final BSAApplet editor = new BSAApplet();
if( args[0].equals( &quot;test&quot; ) ) {
editor.test();
} else {
// Set up the GUI and start the editor.
editor.getData();
}
}
}
 
I didn't actually take a look at the source code (abit too long :p) but the problem lies in the connecting of database. Applets are not allowed to connect to database due to security purposes.

However, you can set the policy file to allow database connection by applets. However, this will risk allowing other applets to access to database too. So the best workaround is to get it signed. If the applet is used in a LAN in your own company, then it would be ok to edit the policy file yourself.

Leon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top