📄 sharkfromscratch2.html
字号:
<html>
<head>
<title>Shark from Scratch II</title>
<link type="text/css" href="sfs.css" rel="stylesheet" id="stylesheet">
<style>
body
{
margin-left: 40px;
margin-right: 40px;
}
</style>
</head>
<body>
<h1>Shark from Scratch II - Starting the Engine and Logging in.</h1>
<p>In this document, I am going to walk through the creation of some sample code. As before, I am not going to cover
any topics not directly related to the subject. You will have to know how to write, compile and execute Java programs.
Additionally, you need to have a Shark build running and be able to use the administrator application that comes supplied
with Shark.</p>
<p>As before, I have the following setup:
<ol>
<li>MySQL - I'm using 4.0.16-nt </li>
<li>MySQL Connector - I have mysql-connector-java-3.0.11-stable-bin.jar</li>
<li>Ant 1.6.x - Not required... but I've provided a build file....</li>
<li>Java - I'm using the Sun j2sdk version 1.4.2_04</li>
<li>CVS, Winzip, etc. as needed</li>
</ol>
</p>
<p>And my Shark build is at C:\Shark after I do a
<pre> make -install</pre>
</p>
<p>The strategy is to iteratively build up some classes to demonstrate how to get around a bit in Shark. The interface will be
a command line out of a main method. The idea is to get the new user started and somewhat comfortable with coding for Shark.</p>
<p>The code will be excessively pedantic and will be overly commented. It will NOT be production quality. So don't even think about
basing your killer app on this code. Also, there will be generated HTML pages of the source so you can
just follow along in a different browser window.</p>
<h3>Files</h3>
<p>You can read this and click through to links that take you to the source, which was generated by the outstanding Java2Html (
<a href="http://www.java2html.com">www.java2html.com</a> ). Additionally, I have provided the sources and ant build files that I used to
create all of this. They can be found here (TODO: Link up the SFSII Zip file). </p>
<p>Click <a href="./code/index.html" target="_new">here</a> to open the browsable source in a new window.</p>
<h2>Starting Shark</h2>
<p>Starting Shark is really as simple as this:</p>
<pre> Shark.configure(new Properties("Shark.conf")); </pre>
<p>That said, the majority of the first timer questions on the Shark mailing list seem to be about starting / configuring shark. You have to
configure Shark (see Shark from Scratch TODO add link). Then you have to make sure that all of the necessary jars are available to your
code. The code we build here will NOT live in the Shark directory. This is intentional so that we can experience the joy of classpath
problems.</p>
<p>The absolute bare minimum set of jars to start Shark (with my configuration) is this: </p>
<ul>
<li>C:\Shark\lib\sharkkernel-standard.jar</li>
<li>C:\Shark\lib\sharkclientapi.jar</li>
<li>C:\Shark\lib\sharkcommonapi.jar</li>
<li>C:\Shark\lib\sharkinternalapi.jar</li>
<li>C:\Shark\lib\sharkxpdl-standard.jar</li>
<li>C:\Shark\lib\sharkinstancepersistence-dods.jar</li>
<li>C:\Shark\lib\dods-runtime.jar</li>
<li>C:\Shark\lib\util.jar</li>
<li>C:\Shark\lib\sharkutilities-dods.jar</li>
<li>C:\Shark\lib\mysql-connector-java-3.0.11-stable-bin.jar</li>
<li>C:\Shark\lib\sharkrepositorypersistence-dods.jar</li>
<li>C:\Shark\lib\sharkscripting-standard.jar</li>
<li>C:\Shark\lib\sharkutilities-map.jar</li>
<li>C:\Shark\lib\sharkutilities-misc.jar</li>
<li>C:\Shark\lib\xercesImpl.jar</li>
<li>C:\Shark\lib\xml-apis.jar</li>
</ul>
<h3>Building the sample</h3>
<p>Because this one is small, I have inlined the code here for perusal. Of course, all of the source code and a build script are provided.
Look here <b><font size="4">(TODO link to source zip)</font></b>. This first example is found in the <code>first</code> package if you
are following along at home.</p>
<p>run "ant -p" to see the targets available. If you don't use c:\Shark as your install directory, you will have to change the
<code>shark.dir</code> property in the build script. The class we are running is called <code>Simple</code>. Before we run
it... lets have a look at the code.</p>
<pre>package first;
import org.enhydra.shark.Shark;
import java.util.Properties;
import java.io.FileInputStream;
/**
* Does nothing more than start the Shark engine.
*/
public class Simple
{
public static void main(String[] args)
{
try
{
Properties props = new Properties();
props.load(new FileInputStream("C:\\Shark\\Shark.conf"));
System.out.println("Enginename from Shark.conf is : " + props.getProperty("enginename"));
// Make SURE every instance of shark that connects to the same database uses a different
// enginename.
props.setProperty("enginename", "rross");
Shark.configure(props);
}
catch (Exception e)
{
// It's not like I am going to revive the thing if it crashes...
e.printStackTrace();
}
}
}
</pre>
<p>Pretty simple. Load the properties file, supply a different engine name and hand the properties object over to Shark.</p>
<h3>Start your engines</h3>
<p>To run it, just type "ant runFirst" at the command prompt. This will be how we run all of the targets, BTW. When you do, you should see
the following output (roughly). </p>
<pre>C:\dev\SharkFromScratch>ant runFirst
Buildfile: build.xml
runFirst:
[java] Enginename from Shark.conf is : Shark
[java] SharkEngineManager -> Shark engine is being initialized ...
[java] File dodsConf.xml not exists on path: 'dodsconf' using default insted
[java] Shark -> Initializing of shark engine has finished
[java] Shark -> Initializing lasted 5 [s]
[java] Shark -> firstEngine ready and waiting ...
BUILD SUCCESSFUL
Total time: 6 seconds</pre>
<p>Everything looks good, with one exception. "File dodsConf.xml not exists on path: 'dodsconf' using default insted". Ignoring the
grammar and spelling issues, this is saying that it could not find my copy of dodsConf.xml. If I do a quick search in my Shark directory,
I see that dodsConf.xml does indeed exist. What is happening is that the Shark.conf file has relative paths by default and when you
run in the directory that holds the build file (C:\dev\SharkfromScratch for me), Shark expects to find all of it's conf and other info here.
Notice that it still started up. I doubt however, that it would have been happy for long.</p>
<h3>Fixing up the configuration</h3>
<p>Rather than edit the Shark.conf file on disk, I am going to edit the properties handed to the Shark engine at start time. This gives
me a little better control over what goes in, and allows me to continue to use the 'official' Shark.conf for the applications like
runsa.bat without tweaking the file all day long.</p>
<p>Simply put, I am going to load the properties file and prepend the Shark install directory to all of the relative file names in the
properties before I give them to Shark.</p>
<p>In <code>second.Simple</code>, you can see this happen. It's not magic, and I've just hardcoded the Shark install path, but it does
get the job done.</p> <b><font size="4">TODO : Link to J2H output (the code html) here.</font></b>
<p>Running this, you should get the following output (or very similar): </p>
<pre>C:\dev\SharkFromScratch>ant runSecond
Buildfile: build.xml
runSecond:
[java] Fixing up Shark config
[java] SharkEngineManager -> Shark engine is being initialized ...
[java] Shark -> Initializing of shark engine has finished
[java] Shark -> Initializing lasted 5 [s]
[java] Shark -> secondEngine ready and waiting ...
BUILD SUCCESSFUL
Total time: 6 seconds</pre>
<p>As you can see, Shark is no longer complaining about not being able to find dodsConf.xml. At this point, we seem to have started
Shark properly</p>
<h2>Digging around in Shark</h2>
<p>Now that we can start shark, lets see what we have got. The <code>third</code> package is our first real attempt to build a program. The <code>Workflow</code>
class provides a method called <code>init</code> that encapsulates the work we have done so far. </p>
<h3>Logging in </h3>
<p>Next up, we log in. Actually, what we do is get a SharkConnection object and call it's connect method. This code snippet is from the
<code>login</code> method.</p>
<pre>SharkConnection conn = Shark.getInstance().getSharkConnection();
try
{
conn.connect(userName, password, props.getProperty("enginename"), null);
}
</pre>
<p>That fourth parameter to the connect call is 'scope' but last time I checked, it wasn't connected to anything. Using the "runThird" target
in the build.xml provided, we should see something very like :</p>
<pre>C:\dev\SharkFromScratch>ant runThird
Buildfile: build.xml
runThird:
[java] Fixing up Shark config
[java] SharkEngineManager -> Shark engine is being initialized ...
[java] Shark -> Initializing of shark engine has finished
[java] Shark -> Initializing lasted 5 [s]
[java] Shark -> thirdEngine ready and waiting ...
BUILD SUCCESSFUL
Total time: 6 seconds</pre>
<p>The observant amongst you will have notice that no new information was provided when we logged in. This is fine, the connection is not
null and no exceptions were thrown, so we are good to go.</p>
<p>It is important to note that a brand spanking new database, after a call to <code>recreateDB</code>, will not actually have any users
in the database. If you have not already done so, you can run the Shark Swing Application (<code>C:\Shark\runsa.bat</code> for me) and login
as "admin" with the password "enhydra". This will create the admin user. In fact, if you login to the swing app with any unknown name, it will
create a new user with the given name and password.</p>
<h3>Classpaths</h3>
<p>A quick note about classpaths. In the build.xml file provided, the same class path is used for all of the targets, but as you can
see, I have commented it so that you can tell what files were required at any given point. Not all of the files that a Shark 'make' produces
will be necessary for a given install, so maybe that will help you trim down your classpath a little.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -