Wednesday, July 24, 2013

NetBeans 7.4 Tip of the Day: Adding A Custom License to NetBeans

I am really enjoying the new NetBeans 7.4 Beta. It features a new mechanism to add license headers to your project. It also allows you to switch the license easily in your project as well. In this example I will add a new BSD 3-Clause License "New BSD" to the Template Manager. This makes it easier to create a project using any variety of license that is required.

We will be adding the following file license-bsd_3.txt.

<#if licenseFirst??>
${licenseFirst}
</#if>
${licensePrefix}Copyright ${date?date?string("yyyy")} ${project.organization!user}.
${licensePrefix}All rights reserved.
${licensePrefix?replace(" +$", "", "r")}
${licensePrefix}Redistribution and use in source and binary forms, with or without
${licensePrefix}modification, are permitted provided that the following conditions are met:
${licensePrefix?replace(" +$", "", "r")}
${licensePrefix}* Redistributions of source code must retain the above copyright notice,
${licensePrefix}  this list of conditions and the following disclaimer.
${licensePrefix}* Redistributions in binary form must reproduce the above copyright notice,
${licensePrefix}  this list of conditions and the following disclaimer in the documentation
${licensePrefix}  and/or other materials provided with the distribution.
${licensePrefix}* Neither the name of the ${project.organization!organization} nor the names of its contributors
${licensePrefix}  may be used to endorse or promote products derived from this software without specific
${licensePrefix}  prior written permission.
${licensePrefix?replace(" +$", "", "r")}
${licensePrefix}THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
${licensePrefix}AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
${licensePrefix}IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
${licensePrefix}ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
${licensePrefix}LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
${licensePrefix}CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
${licensePrefix}SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
${licensePrefix}INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
${licensePrefix}CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
${licensePrefix}ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
${licensePrefix}POSSIBILITY OF SUCH DAMAGE.
<#if licenseLast??>
${licenseLast}
</#if>
Copyright (c) <YEAR>, <OWNER>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
view raw NEW-BSD.txt hosted with ❤ by GitHub
Note: The license file must start with the prefix license- and end with .txt.

  1. Download license-bsd_3.txt, or create your own.
  2. Open Tools → Templates → Licenses
  3. Click Add and add the license file. It will be installed as license-bsd_3.txt
  4. Click Rename and rename the file to BSD 3-Clause License.
  5. Create a new Maven project. Open the properties for the project and go to License Headers. Select use global license and select our new license.
  6. Create a Java class and the license should contain our new license.
    /*
    * Copyright 2013-2017 John Yeary <jyeary@bluelotussoftware.com>.
    * All rights reserved.
    *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are met:
    *
    * * Redistributions of source code must retain the above copyright notice,
    * this list of conditions and the following disclaimer.
    * * Redistributions in binary form must reproduce the above copyright notice,
    * this list of conditions and the following disclaimer in the documentation
    * and/or other materials provided with the distribution.
    * * Neither the name of the Blue Lotus Software, LLC nor the names of its contributors
    * may be used to endorse or promote products derived from this software without specific
    * prior written permission.
    *
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    * POSSIBILITY OF SUCH DAMAGE.
    */
    view raw license_output hosted with ❤ by GitHub

UPDATE: I was asked via twitter about the organization property. This is set from Tools → Templates → Settings. Add a property called organization.

JSF 2.x Tip of the Day: RichFaces 4.3.x Resource Mapping

I have a great opportunity to do different things at work. I get to help solve some really interesting issues around web development. This is particularly true with JSF. In the last few days we have been working on some issues with WebSphere using JSF. In particular, there is an issue with Drag-and-Drop (DnD) on RichFaces with MyFaces on WebSphere. We figured out that there is a possible fix we can implement by fixing the RichFaces DnD Javascript library.

So.... the question is how do we implement the changed library without having to compile a custom version of RichFaces, or modifying the files to load our changed script and prevent the bad script from loading, or implementing our own ResourceHandlerWrapper.

Well there are a number of alternative solutions, but if you are using RichFaces  there is a simple choice: using the Resource Mapping feature of RichFaces  This feature allows you to remap resources to point to alternative resources. This is a really helpful and convenient feature, and solves our problem of modified DnD JS for our RichFaces application.

The details of how to use it are mentioned on the Resource Mapping page, but only from a very high level, and there is no example implementation. There is a blog post Re-routing JSF resource requests with RichFaces Resource Mapping from Lukas Fryc which details more functionality than the guide, but there is no complete example. I am going to rectify that with this post.

The code for the example that follows is located here: richfaces-resource-mapping.

The first thing you need is a static-resource-mappings.properties file. This tells the RichFaces ResourceServlet what the alternative mappings should be. In the example application above my mappings look like the following.


As you can see, I am remapping images to remote IP addresses, and the jquery.js file to an alternative location on the file system. This alternative remapping of jquery.js file allows us to replace the JQuery instance that comes with RichFaces with our own alternative version.

The static-resource-mappings.properties needs to be located in the META-INF/richfaces directory, or one of the other alternatives as noted in Resource Mapping guide. This can be easily accomplished in Maven using the code below:

1
2
3
4
5
6
<resources>
    <resource>
        <directory>src/main/resources/richfaces</directory>
        <targetpath>META-INF/richfaces</targetPath>
    </resource>
</resources>
This allows us to copy the files in the resources directory to META-INF/richfaces automatically while building.

That is pretty much it. How about that for being easy.

Here is what the output looks like in the example application: richfaces-resource-mapping.

RichFaces Resource Mapping

Popular Posts