Problem Adding a Network Printer Under Windows 7 on a Samba Share

Ah, Windows.  It still has it’s old, quirky, you-just-can’t-wait-for-MicroSoft-to-go-out-of-business charm.

But seriously, if you are having trouble adding a printer on a Samba share on a Linux host try the following:

  • After you authenticate to the Samba share, make sure that you actually open up a window displaying the contents of the home directory.  For some reason, just authenticating to the Samba share will not get you access to the Samba servers list of shared printers.
  • If the searching for printers window hangs without displaying the Samba share’s printers, hit the cancel button, close the window and try it again.

Syntax for Dynamically Generating JSON in JavaScript to be Output to HTML

Should you find yourself in a situation where you want to call a function (or method) that accepts JSON data parameters that you want to actually print to the browser to be invoked on some sort of callback (onchange, onclick, etc).

Given the following function:

doSomething(JSONparam) {
    // Something happens in here . . .
}

Here is the syntax for rendering an onclick link that is generated in JavaScript and then output to the DOM:

var $markup = ‘<a href=”some/url.html” onclick=”doSomething({\’key1\’:\’value’\, \’key2\’:\’value\’}); return false;”>Some link</a>’;

Simple Solution for JavaScript Object Packages and Namespaces

Most OO programers are familiar with packages.  This enables programmers to group like classes together in a logical order and provide some namespace to avoid class name collisions.

I have seen a variety of JavaScript sugar implementations that are mostly unintelligible.

I like things that are simple and easy to understand.

So, if you have a JavaScript class that looks like:

/**
 * Constructor
 */
com.ryanchapin.Base = function() {

    //
    // Constructor does something here . . .
    //
}

//—————————————————————————-
// Properties:
//

/**
 * Some property
 */
com.ryanchapin.Base.prototype.someProperty;

//—————————————————————————-
// Methods:
//

com.ryanchapin.Base.prototype.doSomething = function() {

    //
    // Do something here . . . .
    //
}

Then all you need to do is add something similare to the following as the top-most block of JavaScript on the page:

<script type=”text/javascript”>

            // Instantiate ‘package/namespace’, containing object
            var com = new Object();
            com.ryanchapin = new Object();

            // Then you can instantiate your objects as follows:
            var baseInst = new com.ryanchapin.Base();

</script>

This will create the ‘container’ object in which your class definitions will reside and help reduce the use of global variables as well as class name and method name collisions.

Solution for ‘NX service is not available’

When first installing NXServer I have seen a number of occastions when the initial run of

# /usr/NX/bin/nxserver –install

Does not necessarily work correctly and simply running the following commands has worked.

# /usr/NX/bin/nxsetup –uninstall –purge –clean

then

# /usr/NX/bin/nxsetup –install –setup-nomachine-key

If the previous uninstall/install command doesn’t work, try rpm -e nx*.rpms and reinstalling the rpms before trying to run the nxserver –install command again.

Resources for Understanding Object-Oriented JavaScript

Following are some good articles/tutorial on OO JavaScript:

Creating a MD5 Hash in Java From Either a String or the Byte Value of a Long

Following is a quick example of how to generate an MD5 Hash (SHA-256, SHA-512 or any other supported hash algorithm) of either a String or the byte value of a long.  You should be able to adapt for your particular application:

package com.ryanchapin.util;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class HashGenerator {

    public static String createHash(long input, String hashAlgorithm) {
        // Generate a byte array from the long
        // Extract the byte value of the long
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);

        try {
            dos.writeLong(input);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            dos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        byte[] byteArray = bos.toByteArray();
        return bytesToHex(computeHashBytes(byteArray, hashAlgorithm));
    }

    public static String createHash(String input, String hashAlgorithm) {
        // Generate a byte array from the input String.
        byte[] inByteArray = null;
        try {
            inByteArray = input.getBytes(“UTF-8”);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return bytesToHex(computeHashBytes(inByteArray, hashAlgorithm));
    }

    private static byte[] computeHashBytes(byte[] inputBytes, String hashAlgorithm) {
        // Instantiate a MessageDigest instance configured with the desired
        // algorithm.
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance(hashAlgorithm);
        } catch(NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        // This isn’t necessary in this context, but should this
        // be refactored to use the MessageDigest as a member this
        // enables the reuse of the same MessageDigest instance.
        md.reset();
        md.update(inputBytes);
        return md.digest();
    }

    private static String bytesToHex(byte[] hashBytes) {

        // Convert the hashBytes to a String of hex values
        StringBuilder retVal   = new StringBuilder();
        StringBuilder hexValue = new StringBuilder();

        for (byte hashByte : hashBytes) {
            // Flush our StringBuilder to be used as a container for the
            // hex value for each byte as it is read.
            hexValue.delete(0, hexValue.length());
            hexValue.append(Integer.toHexString(0xFF & hashByte));

            // Add a trailing ‘0’ if our hexValue is only 1 char long
            if (hexValue.length() == 1) {
                hexValue.insert(0, ‘0’);
            }
            retVal.append(hexValue);
        }
        return retVal.toString();
    }

    public static void main(String[] args) {

        // Hash using MD5
        System.out.println(createHash(“12345”, “MD5”));
        System.out.println(createHash(12345L, “MD5”));

        // Hash using SHA-256
        System.out.println(createHash(“12345”, “SHA-256”));
        System.out.println(createHash(12345L, “SHA-256”));
    }
}