04 August 2011

Using PHDCC CodeModule with javascript

phdcc Director John Cant writes:

My use of DNN has become -

[a] Maintain any HTML in a text file / copy and paste it into the basic editor / convert to raw / save. (Online editors are too hard to use and keep slim)

[b] Put any functionality into a phdcc.CodeModule

Much valuable functionality - jquery-ui / google maps / facebook / janrain / plupload / galleria - is available as javascript libraries. To get them onto a DNN Page, I have created a template CodeModule as follows.

ASP.NET gives you limited control over where in the page javascript can be inserted - RegisterClientScriptBlock inserts somewhere near the top; RegisterStartupScript somewhere near the bottom. All I can say is - the template works on all browsers tested.

In this example, I load the jquery-ui library at the top, and daisy chain the DNN onload handler at the bottom. The javascript could of course be located in a separate file.

In the onload handler, I create an invisible empty jquery-ui dialog and initialise a jquery-ui accordion. I bind the submit button of a PHDCC form to a handler and examine one of the form fields to give feedback via the dialog.

I add the CodeModule somewhere out of the way on the page - the bottom pane is a good place. Once there, accordions and dialogs can be used anywhere on the page.

If you need any help with this or any of the suggested libraries, please get back to us.

<%@ Control Language="C#" ClassName="JCformWidgets" %>

<script runat="server">

//--------------------------------------
private void loadJS()
{
string scriptKey = "JCjsIncludes:" + this.UniqueID;
if( Page.IsStartupScriptRegistered( scriptKey) || Page.IsPostBack) return;

ClientScriptManager cs = Page.ClientScript;
Type cstype = this.GetType();

// ----------------------------
// load JS files at the top of the page
// ----------------------------
string scriptBlock = "\n";

scriptBlock += "<link type='text/css' href='/jqui/jquery-ui-1.8.14.custom.css' rel='Stylesheet'>\n";
scriptBlock += "<script src='/jqui/jquery-ui-1.8.14.custom.min.js' type='text/javascript'>";
scriptBlock += "</";
scriptBlock += "script>\n"; // avoid microsoft parsing bug

cs.RegisterClientScriptBlock( cstype, scriptKey, scriptBlock);

// ----------------------------
// load onload handler etc. at the bottom of the page
// ----------------------------
string startupScript =
@"<script language='JavaScript'>
<!--

function JConload(){

var oldOnLoad = window.onload;

var myDialog = $('<div></div>').dialog( {autoOpen: false} );

this.newOnload = function(){

// call existing handler
if(typeof oldOnLoad === 'function'){
oldOnLoad();
}

// -------------------------------------

$(function() {
$( '#accordion' ).accordion();
});

// -------------------------------------

$('#' + 'dnn_ctr422_ViewForm_btnSubmit').bind('click', function() {

var theseSkills = $( '#' + 'dnn_ctr422_ViewForm_Group_157_400_Question_157_400_1563_form_157_text_1563').val();
var thisDialogText = '';

if( theseSkills.length <= 0) {
thisDialogText = 'No skills given<br>Please try again.';

myDialog.html( thisDialogText);
myDialog.dialog( 'option', 'title', 'Things you forgot ...' );

myDialog.dialog( 'open');
return false;
}
});

// -------------------------------------

};

}

(function(){
var JCOL = new JConload();
window.onload = JCOL.newOnload;
}());

// -->";

startupScript += "</sc";
startupScript += "ript>"; // avoid microsoft parsing bug

cs.RegisterStartupScript( cstype, scriptKey, startupScript);
}

//--------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
loadJS();
}

</script>


12 May 2011

Scheduled task ping for plesk in Windows

I've been moving several domains to a Windows cloud hosting package at http://www.webhosting.uk.com/. I'm currently using Cloud Beginner hosting that gives 10 domains and a dedicated IP address for £12/month. This package lets us run DotNetNuke (DNN) sites - and fast too as there is not much contention on the included SQL Server databases. The cloud is UK based so the ping time is less from the UK and nearby.

The account is run using Windows plesk which lets you control most things. However intervention is often needed from the webhosting.uk.com staff. They are usually very responsive and competent on their Live Chat service. Typical requests include setting ASP.NET modify permissions for the Network Service user and setting Application Starting Points. You need to insist that you get the dedicated IP address if you've asked for it. Email is included using the Horde web client, which isn't great; perhaps using Google Apps for email might be the best solution. No web stats package is included. Nor are file or database backups.

For one domain, one task that I needed to do was to set up scheduled tasks to ping various URLs. While this could be done externally, I decided to use the Plesk scheduled task feature. When FTPing in, the main site is at /httpdocs/. The scheduled task VBS scripts should be placed in /cgi-bin/

Using a couple of resources on the Internet, I put together this script, called pingurls.vbs:

Call DoScheduledTask()

Sub DoScheduledTask()
On Error Resume Next

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("E:\inetpub\vhosts\mydomainname.com\cgi-bin\urls2ping.txt", ForReading)

Dim URL
Do Until objFile.AtEndOfStream
URL = objFile.ReadLine
if Len(URL)>0 then PingURL(URL)
Loop
objFile.Close

end sub

sub PingURL(URL)

Dim RequestObj
Set RequestObj = CreateObject("Microsoft.XMLHTTP")
'Open request and pass the URL
RequestObj.open "POST", URL , false
'Send Request
RequestObj.Send
'cleanup
Set RequestObj = Nothing

End Sub

This script reads a text file urls2ping.txt, also in the /cgi-bin/ directory. Each line contains a URL to be pinged.

How do you know what absolute path to use to get to the text file? This ASP.NET code, say in MyPath.aspx, will tell you the path it is running from:
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load()
lblPath.Text = Request.MapPath("~")
End Sub
</script>
<html>
<head>
<title>Web app path using Request.MapPath</title>
</head>
<body>
<asp:Label ID="lblPath" Runat="server" />
</body>
</html>


The final stage is to set up the scheduled task in plesk. This is straight forward using [Scheduled Tasks] then [Add New Task]. The Path to Executable file should be similar to that used above, ie E:\inetpub\vhosts\mydomainname.com\cgi-bin\pingurls.vbs. Set the times you want the task to run.

20 November 2010

Installing Tomcat onto Ubuntu Apache2

This post describes how to install Tomcat 6 on an Ubuntu 10.10 server and connect it up with the Apache2 server using the jk module. Although there are plenty of posts about this, none gave a full list of instructions; this is what I aim to provide - let me know if you have any corrections.

I assume that you have Apache2 installed.

You will need to do most of the following actions at a root shell prompt. Get this from your login using: sudu su - You will need to edit various text files. You can do this using vi at the shell prompt. However I usually do this on my local computer using my preferred editor, transferring files to and fro using FTP.

Installing Tomcat

This command gets Tomcat 6 - and Java if need be:
aptitude install tomcat6

I'm not sure if that gets all the Tomcat files, so enter this to get them all:
apt-get install tomcat6 tomcat6-admin tomcat6-common tomcat6-user tomcat6-docs tomcat6-examples

On my system, Java was installed to here: /usr/lib/jvm/default-java/

You now need to set up Java environment variables in text script file /etc/environment
Add this to the PATH variable: /usr/lib/jvm/default-java/bin after a colon separator. Add these lines:

JAVA_HOME=/usr/lib/jvm/default-java
JDK_HOME=/usr/lib/jvm/default-java

You will now need to reboot so these changes are used.

Tomcat should now be running. You can start, stop or restart it as follows:

/etc/init.d/tomcat6 stop
/etc/init.d/tomcat6 status
/etc/init.d/tomcat6 restart

or more easily like this:

service tomcat6 restart

Tomcat should now be running on port 8080 at your host, eg http://www.example.com:8080/ If you go there, you should see an It works page. To test it further, I put a JSP file in here: /var/lib/tomcat6/webapps/ROOT/ and saw that it ran OK. I then put a servlet WAR file in /var/lib/tomcat6/webapps/. This was soon expanded automatically by Tomcat and I was able to navigate to the servlet directory (still on the 8080 port).

You may find it useful to run the manager and host-manager Tomcat applications. To enable these you need to edit the Tomcat configuration files in /etc/tomcat6/. Edit tomcat-users.xml to include the following:

<role rolename="manager"/>
<role rolename="admin"/>
<user name="admin" password="secret_password" roles="manager,admin"/>

You should now be able to access these apps at /manager/html and /host-manager/html. You will need to enter your credentials; possibly a couple of times on first access.

The Tomcat logs are at /var/log/tomcat6/. By default Tomcat creates a different log file for each day. Keep an eye on these, and the disk space they consume.

Connecting Tomcat to Apache

Hopefully you already know these commands to test your Apache configuration and restart Apache. You'll need these later.

apache2ctl configtest
apache2ctl restart

First, tell Tomcat to listen out for connections from Apache. Edit /etc/tomcat6/server.xml to uncomment this line:

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

Restart Tomcat.

Install the jk module that acts as the connector between Apache and Tomcat:
apt-get install libapache2-mod-jk

You should see a file called jk.load in /etc/apache2/mods-available/. I edited this to contain:

LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so
JkWorkersFile /etc/libapache2-mod-jk/workers.properties
JkLogFile /var/log/apache2/mod_jk.log
JkShmFile /var/log/apache2/jk-runtime-status
JkLogLevel info

You can also add JkMount instructions here, but I found that I needed to put them in each VirtualHost file in /etc/apache2/sites-available/. Eg in my default VirtualHost file I added:

JkMount /*.jsp ajp13_worker
JkMount /manager ajp13_worker
JkMount /manager/* ajp13_worker
JkMount /host-manager ajp13_worker
JkMount /host-manager/* ajp13_worker

An alternative is to define JkMount commands in jk.load and add a JkMountCopy command to copy these settings to all virtual hosts.

You now need to edit the jk worker file /etc/libapache2-mod-jk/workers.properties. Make sure it has code as follows, ie to tell jk to use the Tomcat connection we defined earlier:

worker.list=ajp13_worker
worker.ajp13_worker.port=8009
worker.ajp13_worker.host=localhost
worker.ajp13_worker.type=ajp13

Restart apache.

I was now able to access the Tomcat manager here: http://www.example.com/manager/html.

Edit your other VirtualHost files to expose any other apps that you need.

You'll find the Apache and jk logs in here: /var/log/apache2/. As above, keep an eye on these files and the disk space they use.

If you see "missing uri map" in log file mod_jk.log then you need to define JkMount in your VirtualHost.

As a final task, if you wish, disable the Tomcat 8080 port by editing /etc/tomcat6/server.xml and then restart Tomcat.