Archive for the ‘Java’ Category

PHP-on-Tomcat (Part II)

Sunday, December 13th, 2009

The original goal of being able to run PHP on top of Tomcat was to allow simple development of PHP based applications like WordPress and/or bbPress. To get these working I needed a database.

The wildly popular mySQL database has a nice installer for OS X. Mine installed to /usr/local. I did the install ages ago and forgot what the “root” database user password was. Ooops! No matter, there are some great resources online talking about how to go about resetting permissions. I opted for the incredibly easy, but utterly insecure method (from inside the terminal) :

  1. Stop the existing server
  2. Restart telling it to turn off permissions: mysqld_safe --skip-grant-tables --user=_mysql &
  3. Jump into the mysql shell application: mysql -u root mysql
  4. Change the “root” password: UPDATE mysql.user SET Password=PASSWORD('#########') where user='root';
  5. Persist privilages: FLUSH PRIVILEGES;

Next on the list was exposing my mySQL database to the Tomcat hosted PHP code. Step 1 was making sure I have mySQL JDBC drivers installed, Step 2 was creating a JNDI datasource in the context, and step 3 was getting the PHP to use it.

The officially supported mySQL Connector/J was a quick download. The JAR file for it was dropped into the $CATALINA_HOME/lib directly. Good to go. The context file was more of a challenge, under the $CATALINA_HOME/conf/Catalina/localhost I created an XML config file that matched the name of my webappphp.xml – where I declared the JNDI datasource. It took some tweaking and reading around but finally ended up coming together:

<Context reloadable="true">
    <Resource
        name="jdbc/mydata"
        auth="Container"
        type="javax.sql.DataSource"
        username="root"
        password="#########"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/test"
    />
</Context>

The documentation for Quercus states

Scripts can use the jndi name directly:

<?php

  // standard PHP
  //mysql_connect($host, $username, $password, $dbname);

  // using JNDI lookup
  mysql_connect("java:comp/env/jdbc/myDatabaseName");

?>

But that doesnt help me to run WordPress as I’m not about to go modifying their code. Never fear, there’s another option. The documentation for Quercus also says that you can put an entry into the WEB-INF/web.xml file that forces all database connections to go through the same underlying JNDI datasource:

.
.
.
    <!--
      Tells Quercus to use the following JDBC database and to ignore the
      arguments of mysql_connect().
    -->
    <init-param>
      <param-name>database</param-name>
      <param-value>jdbc/test</param-value>
    </init-param>
.
.
.

So, a quick update to point at my own datasource and wall was ready to go. I downloaded and unzipped wordpress into the PHP webapp directory, pointed a browser at http://localhost:8080/php/wordpress and found myself stepping through the famous 5 minute install. Less than 5 minutes later I was looking at the main page of a fully-functioning WordPress install.

PHP-on-Tomcat

Saturday, December 12th, 2009

I am generally a Java programmer (if my day-job is to be believed) and hacking my way through Apache configuration isnt my idea of a fun evening. For some odd reason my trusty old black MacBook hasnt had the best of times when I try to enable “web sharing” and then try executing PHP scripts – all I get is a dump of the source. Time for a creative solution to the problem!

The folks that created the Resin App Server have a 100% Java implementation of PHP 5 released under the Open Source GPL license, known as Quercus. So step 1 was to download the latest release that will run on non-Resin app servers: quercus-4.0.1.war.

This was the point where I also realized that I dont have an up-to-date install of Apache Tomcat on my system either. Step 2 was to go download Tomcat 6.0.20 and install it.

Many applications need to know where Tomcat lives, and that means setting the CATALINA_HOME environment variable to point to the install. In a terminal I edited the global startup file for the terminal: sudo vi /etc/bashrc to add the line:

CATALINA_HOME=/Users/paul/Documents/Devt/3rd-party/apache-tomcat-6.0.20

Step 3 was to $CATALINA_HOME/bin/startup.sh to start tomcat and then point a browser at http://localhost:8080/quercus-4.0.1/ where I saw confirmation that all was well:

Testing for Quercus™…

Congratulations! Quercus™ Open Source 4.0.1 is interpreting PHP pages. Have fun!

I didnt like the URL though, but that’s easily changed. I simply nuked the quercus-4.0.1 directory under $CATALINA_HOME/webapps and renamed the Quercus WAR file “php.war”. I bounced Tomcat and bingo – a “php” directory I could drop files into. As a final test, I created info.php

<?php

    phpinfo();

?>

and hit it on the pretty URL: http://localhost:8080/php/info.php. All displayed correctly. I was off and rolling running PHP on top of Apache Tomcat! Stay tuned for Part II – getting WordPress running on top of PHP running on Tomcat!

WIDTW

Tuesday, May 8th, 2007

(What I Did This Weekend)

There’s a saying “While the cat’s away, the mice will play” and I suppose that applied to this past weekend. While Alison was away at the Maryland Sheep and Wool Festival I decided to “get my geek on” and attend the St Louis Code Camp.

A couple of weeks ago I got a call from the church I volunteer with asking if I would speak at the Sunday service at the Garden Villas retirement home that week. It was Wednesday and I told them that I didn’t feel like I could do justice to a topic with that little preparation time. Honestly speaking, I could probably have done it and that was just a very believable excuse, meaning that I didnt have to move other things around to fit the sermon preparation in. After I hung up it was as though I heard a clear voice say, “Now, if they call back, you need to say yes” so it was NO surprise when I got a call a few minutes later. God has a way of calling us on our excuses – calling our bluff as it were!

So, I was prepared for John (one of the church elders) to ask if I would reconsider the decision, and preach that Sunday. Instead he gave me an extra week offering me the Sunday after. After hearing the internal challenge, how could I refuse?

Last week I had a call in the middle of this week from my own church pastor asking if I would stand-in and do the bible reading in our church service. Sunday looked to be busy. Then I got a call from John saying “oh, never mind, our regular volunteer is able to make it after all” about the preaching opportunity. What do you do at that point, especially when you thought you heard God challenge that you needed to accept the preaching assignment and been working in earnest to prepare a sermon? I was clearly disappointed but then I realized: the end result is something that I’ve wanted to have for a long time now! I am now a full month in-hand (sermon for this month, and the following, planned and prepared) and no longer feel like I am living hand-to-mouth (as it were).

Saturday’s Code Camp was a riot! It was so much fun to get together with other highly technical folks and talk “shop”. We swapped horror stories, victories, advice and news. I bumped into several folks that I used to work with and got the chance to catch up. I ended up being a groupie to Kyle Cordes, of Oasis Digital Solutions Inc, as I went to both of his talks (“Selling your Software as a Hosted Service” and “Flying Boxes – a case study of a filthy rich client user interface”). Somewhere during the “Object Oriented JavaScript” talk my brain announced that it was full and would be going offline for maintenance. That was it for the day; I sat out of the final session with other similarly brain-fried folks as we talked about life in the trenches as consultants in large corporate offices. A nice way to wind down.

The weekend as a whole was busy. When it wasn’t busy, and I found myself alone, that was when it got complicated and less-than-pleasant. I’ve been on work related business trips before. I’ve been away a few times, but each time I amazingly busy the whole time and was with other people almost all of it. This weekend was a first. I was alone. Alison was out of town. It felt like I was missing a limb! Preaching at a retirement home brings me into contact with folk who’ve lost a spouse. They use phrases like that but I never had a real sense before of how it must feel. Well, yeah, now I do. I experienced for a weekend (despite knowing deep down that she would be returning) a visceral ache of loss. I experienced a taste of what some people live with the entire rest of their lives. I never realized what Alison meant when she said how much she hated the feeling of me being out of town on business trips. Not until this weekend. Somehow these things went from head to heart, from intellectual assent to personal experience.

We make a point of eating together – even if that means that we hit a fast-food establishment as we both run from one thing to another in a given evening. The worst times for me this weekend were meal-times. I simply couldn’t face eating if it meant sitting at a table alone. I grabbed some pizza at the Code Camp and a couple of random snacks, but mealtimes were the worst. Habits are good. Family mealtimes really bring a sense of “togetherness”. This weekend proves that they have become integral to life itself. Whatever else I face in a day I always know I can look forward to an hour or so of connection. Take that connection time away and I’m lost! I was so grateful, after enduring Friday night and Saturday, that I got invited out to eat by some folks from church for Sunday lunch. I didn’t have to eat alone.

Oh, speaking of Sunday. The sermon I’d prepared and didn’t use (postponed until the end of June now) was an encouragement to really appropriate the forgiveness of God, and the power of regret to hold us back from reaching the goals that God’s set for us and that we were called warts-and-all (from Philippians 3:7-14). In church, our reading was Luke 7:36-48 … a woman whose sins were forgiven after she anoints Jesus’ feet with perfume … and the pastor spoke on really appropriating the forgiveness of God, and the power of regret to hold us back. It was such an encouragement, after such a tough weekend, to feel like I’d heard God’s heartbeat for his people. Whatever my mixed bag of motives are, it felt like a nice confirmation that I had heard correctly, I had chosen the right topic, and to keep pressing on & not second-guessing myself.

What a weekend!

Ohh, Shiny!

Saturday, January 27th, 2007

I have always had a strong interest in creating computer graphics. There are a couple of approaches to image generation. One approach is to create software that puts artist tools at the disposal of user, and relies on the artistic talents of the user to create a picture. The other approach applies mathematical modeling to how light propagates through a scene and the software crunches numbers until you get a picture out the other side of the processing pipeline.

The second approach, known as Raytracing is what Hollywood uses to create the photo-realistic special effects in movies. Raytracing involves creating mathematical models using simple geometric objects (spheres, planes, polynomial surfaces, triangles, etc) and building them up (like Lego bricks) to form a scene. You describe the materials all these primitive objects are made from, and how they reflect/absorb light, along with describing where all the lights in the scene are located. The software crunches light rays and voila you have a picture.

I was bored. I went back to a book that I bought in 1993 – “Photorealism and Raytracing in C” by Stephen B. Coy, Mark Finlay, Christopher D. Watkins. I pulled the mathematical formulae from there, whipped up some Java code. I would term this “version 0” of the software. It supports:

  • Ambient, diffuse and specular lighting models
  • Coloured lighting
  • Two primitives: spheres and planes
  • Basic procedural textures

Procedural textures are interesting: instead of the various materials that the primitives are made from being a flat base colour, you rely on some code to tell you what their colour should be at any given position. In the first example the procedural texture generated a checker-board pattern on the infinite plane, and in the second example a very basic implementation of Perlin noise was used to create the smoky effect. Not bad for a “version 0” piece of software!

There’s a lot left to do before the software is even in a state I would like for someone else to see or get their hands on. I am torn: the mathematical description of a sphere can be generalized and used to describe a cylinder and from there, a cone. More general still to the far more abstract “quadratic surface”; one reasonably simple change and I can support a whole class of new primitives! What the software doesn’t do yet is reflection or refraction – all the primitives are shiny plastic right now – what I want to do is model light bouncing around (metallic objects) and passing through (glassy objects).

Still, I am very happy. My physics class taught me that if you mix red and green lighting, and a surface reflects both, then you will see yellow. In the final screenshot you can clearly see the shiny silver ball now reflects both the colours, with a band of yellow at the top. So, upward and onward toward the goal of “version 0.1” where more primitives are supported. Then I’ll get to “version 0.2” where reflection and refraction are modeled. Watch this space!