<commit_before># XDropWizard

A jump-start DropWizard Web Application integrating and demonstrating several useful open source projects such as Yank, Sundial (a Quartz fork), Flot, Bootstrap, AngularJS, HSQLDB, XChart, JUnit, etc. Demonstrates how to serve static content, dynamic content loaded into Freemarker templates, using AJAX and more...

![Screenshot of Dashboard](https://raw.githubusercontent.com/timmolter/XDropWizard/master/etc/xdropwizard.png)

## Features

* [x] Uses Dropwizard for web application
* [x] Uses Bootstrap for looks
* [x] Uses AngularJS for MVC
* [x] Uses Sundial for Job scheduling
* [x] Uses Yank for databases
* [x] Uses XChart for Bitmap charts
* [x] Uses Flot for Javascript charts
* [x] Uses Freemarker for dymanic templating
* [x] Demonstrates working Dropwizard setup
* [x] Demonstrates Dropwizard tasks
* [x] Demonstrates Dropwizard halthchecks
* [x] Demonstrates Dropwizard building and deploying
* [x] Java 7 and up
* [x] Apache 2.0 license

## Banner Generator

If you want DropWizard to print out a banner in the console during app startup, you can add a file called `banner.txt` in `src/main/resources`. Use the following link to generate a banner:

http://www.webestools.com/ascii-text-generator-ascii-art-code-online-txt2ascii-text2ascii-maker-free-text-to-ascii-converter.html

## Running in Eclipse

Run `XDropWizardApplication` in Eclipse. Add a program arg: `server xdropwizard.yml`.

## Terminal

    cd ~/path/to/project/XDropWizard

## Build

    mvn clean package

## Run

    $ java -jar target/xdropwizard.jar server xdropwizard.yml

## Test Basics

    http://localhost:9090/service/hello-world
    http://localhost:9090/admin/
    http://localhost:9090/admin/healthcheck

## Run Tasks

    curl -X POST http://localhost:9090/admin/tasks/gc

## Static Content

Serving static content such as images, html, css, javascript and binary files from you XDropWizard Webservice is possible in addition to the normal JSON resources
typical for a webservice. DropWizard names static content as "Assets" and all you need to do is place them on the classpath in a folder called `assets`. In our case
we simply add the `assets` folder to `src/main/resources` and Maven takes care of adding the folder and its contents to the classpath during the build.

Either your service or your static assets can be served from the root path, but not both. The latter is useful when using Dropwizard to back a Javascript application
as is the case with XDropWizard. To enable it, move your service to a sub-URL. Note that all webservice calls will now need `service` at the root of the URL. This only applies to the
non-admin port however.

    http:
      rootPath: /service/*  # Default is /*

Then use an extended AssetsBundle constructor to serve resources in the assets folder from the root path. `index.htm` is served as the default page. You need to include the following
line of code in the `Service` class in the `initialize` method:

    bootstrap.addBundle(new AssetsBundle("/assets/", "/"));

In order to keep the `assets` folder a bit organized, we can add subfolders to it. Our `assets` folder contains `img`, `js`, and `css` subfolders. Our `assets` folder also contains
a special-case file called `index.htm`. By default, DropWizard serves this as the default HTML page.

### Static Content Access

Finally, once DropWizard is running, you can access the static content via the following URLS:

    http://localhost:9090
    http://localhost:9090/img/favicon.png
    http://localhost:9090/img/logo_60.png
    http://localhost:9090/css/main.css
    http://localhost:9090/sample.html

BTW, the HTML file `index.html` contains all the links referenced in this README file providing a nice overview of all the demonstrated functionality. If you run this
DropWizard application as described above, you should be able to click on all the links displayed on `index.html` at the following URL:

    http://localhost:9090

### Another Option

Another approach is to serve all static content from a webserver such as Apache HTTP or nginx, placed in front of the DropWizard instance. This however has the disadvantage
of spreading your app's content over several places, and the configuration and maintenance is more complex. In certain cases it may make sense though. <NAME> blogged
about how it can be done with nginx [here](http://gary-rowe.com/agilestack/2013/02/13/an-nginx-config-file-for-dropwizard-with-static-content/).

## Sundial

Sundial is a lightweight Java job scheduling framework.

Integrating [Sundial](https://github.com/timmolter/Sundial) into a DropWizard instance requires minimal setup, and once it's all configured and running,
the scheduling and automatic running of jobs is straight forward and stable. For those not familiar with Sundial, it is a simplified fork of [Quartz](http://quartz-scheduler.org/)
developed by Xeiam. A lot of the bloat and confusion of configuring Quartz was removed in creating Sundial and a convenient wrapper around jobs was added to enable
more modular job building and organization. Sundial creates a threadpool on application startup and uses it for background jobs.


## Adding the dropwizard-sundial dependency

Add the **dropwizard-sundial** library as a dependency to your `pom.xml` file:

```xml
<dependency>
    <groupId>com.xeiam</groupId>
    <artifactId>dropwizard-sundial</artifactId>
    <version>0.8.1.0</version>
</dependency>
```


## Create a Job Class

```java
public class SampleJob extends com.xeiam.sundial.Job {

  @Override
  public void doRun() throws JobInterruptException {
    // Do something interesting...
  }
}
```
##  ...with CronTrigger or SimpleTrigger Annotation
```java
@CronTrigger(cron = "0/5 * * * * ?")
```
```java
@SimpleTrigger(repeatInterval = 30, timeUnit = TimeUnit.SECONDS)
```

## Start Sundial Job Scheduler

```java
public static void main(String[] args) {

  SundialJobScheduler.startScheduler("com.xeiam.sundial.jobs"); // package with annotated Jobs
}
```

## Alternatively, Put an XML File Called jobs.xml on Classpath

```xml
<?xml version='1.0' encoding='utf-8'?>
<job-scheduling-data>

    <schedule>

        <!-- job with cron trigger -->
        <job>
            <name>SampleJob3</name>
            <job-class>com.foo.bar.jobs.SampleJob3</job-class>
            <concurrency-allowed>true</concurrency-allowed>
        </job>
        <trigger>
            <cron>
                <name>SampleJob3-Trigger</name>
                <job-name>SampleJob3</job-name>
                <cron-expression>*/15 * * * * ?</cron-expression>
            </cron>
        </trigger>

        <!-- job with simple trigger -->
        <job>
            <name>SampleJob2</name>
            <job-class>com.foo.bar.jobs.SampleJob2</job-class><commit_msg>improved Sundial Integration docs<commit_after># XDropWizard

A jump-start DropWizard Web Application integrating and demonstrating several useful open source projects such as Yank, Sundial (a Quartz fork), Flot, Bootstrap, AngularJS, HSQLDB, XChart, JUnit, etc. Demonstrates how to serve static content, dynamic content loaded into Freemarker templates, using AJAX and more...

![Screenshot of Dashboard](https://raw.githubusercontent.com/timmolter/XDropWizard/master/etc/xdropwizard.png)

## Features

* [x] Uses Dropwizard for web application
* [x] Uses Bootstrap for looks
* [x] Uses AngularJS for MVC
* [x] Uses Sundial for Job scheduling
* [x] Uses Yank for databases
* [x] Uses XChart for Bitmap charts
* [x] Uses Flot for Javascript charts
* [x] Uses Freemarker for dymanic templating
* [x] Demonstrates working Dropwizard setup
* [x] Demonstrates Dropwizard tasks
* [x] Demonstrates Dropwizard halthchecks
* [x] Demonstrates Dropwizard building and deploying
* [x] Java 7 and up
* [x] Apache 2.0 license

## Banner Generator

If you want DropWizard to print out a banner in the console during app startup, you can add a file called `banner.txt` in `src/main/resources`. Use the following link to generate a banner:

http://www.webestools.com/ascii-text-generator-ascii-art-code-online-txt2ascii-text2ascii-maker-free-text-to-ascii-converter.html

## Running in Eclipse

Run `XDropWizardApplication` in Eclipse. Add a program arg: `server xdropwizard.yml`.

## Terminal

    cd ~/path/to/project/XDropWizard

## Build

    mvn clean package

## Run

    $ java -jar target/xdropwizard.jar server xdropwizard.yml

## Test Basics

    http://localhost:9090/service/hello-world
    http://localhost:9090/admin/
    http://localhost:9090/admin/healthcheck

## Run Tasks

    curl -X POST http://localhost:9090/admin/tasks/gc

## Static Content

Serving static content such as images, html, css, javascript and binary files from you XDropWizard Webservice is possible in addition to the normal JSON resources
typical for a webservice. DropWizard names static content as "Assets" and all you need to do is place them on the classpath in a folder called `assets`. In our case
we simply add the `assets` folder to `src/main/resources` and Maven takes care of adding the folder and its contents to the classpath during the build.

Either your service or your static assets can be served from the root path, but not both. The latter is useful when using Dropwizard to back a Javascript application
as is the case with XDropWizard. To enable it, move your service to a sub-URL. Note that all webservice calls will now need `service` at the root of the URL. This only applies to the
non-admin port however.

    http:
      rootPath: /service/*  # Default is /*

Then use an extended AssetsBundle constructor to serve resources in the assets folder from the root path. `index.htm` is served as the default page. You need to include the following
line of code in the `Service` class in the `initialize` method:

    bootstrap.addBundle(new AssetsBundle("/assets/", "/"));

In order to keep the `assets` folder a bit organized, we can add subfolders to it. Our `assets` folder contains `img`, `js`, and `css` subfolders. Our `assets` folder also contains
a special-case file called `index.htm`. By default, DropWizard serves this as the default HTML page.

### Static Content Access

Finally, once DropWizard is running, you can access the static content via the following URLS:

    http://localhost:9090
    http://localhost:9090/img/favicon.png
    http://localhost:9090/img/logo_60.png
    http://localhost:9090/css/main.css
    http://localhost:9090/sample.html

BTW, the HTML file `index.html` contains all the links referenced in this README file providing a nice overview of all the demonstrated functionality. If you run this
DropWizard application as described above, you should be able to click on all the links displayed on `index.html` at the following URL:

    http://localhost:9090

### Another Option

Another approach is to serve all static content from a webserver such as Apache HTTP or nginx, placed in front of the DropWizard instance. This however has the disadvantage
of spreading your app's content over several places, and the configuration and maintenance is more complex. In certain cases it may make sense though. <NAME> blogged
about how it can be done with nginx [here](http://gary-rowe.com/agilestack/2013/02/13/an-nginx-config-file-for-dropwizard-with-static-content/).

## Sundial

Sundial is a lightweight Java job scheduling framework.

Integrating [Sundial](https://github.com/timmolter/Sundial) into a DropWizard instance requires minimal setup, and once it's all configured and running,
the scheduling and automatic running of jobs is straight forward and stable. For those not familiar with Sundial, it is a simplified fork of [Quartz](http://quartz-scheduler.org/)
developed by Xeiam. A lot of the bloat and confusion of configuring Quartz was removed in creating Sundial and a convenient wrapper around jobs was added to enable
more modular job building and organization. Sundial creates a threadpool on application startup and uses it for background jobs.


## Adding the dropwizard-sundial dependency

Add the **dropwizard-sundial** library as a dependency to your `pom.xml` file:

```xml
<dependency>
    <groupId>com.xeiam</groupId>
    <artifactId>dropwizard-sundial</artifactId>
    <version>0.8.1.0</version>
</dependency>
```

## Integrate Sundial into you App


1. Add the Sundial Bundle in the initialize method.

```java
@Override
public void initialize(Bootstrap<XDropWizardApplicationConfiguration> bootstrap) {

    ...

    bootstrap.addBundle(new SundialBundle<XDropWizardApplicationConfiguration>() {

      @Override
      public SundialConfiguration getSundialConfiguration(XDropWizardApplicationConfiguration configuration) {
        return configuration.getSundialConfiguration();
      }
    });
}
```

2. Add `SundialConfiguration` to your configuration file.

```java
// Sundial
@Valid
@NotNull
public SundialConfiguration sundialConfiguration = new SundialConfiguration();

@JsonProperty("sundial")
public SundialConfiguration getSundialConfiguration() {

    return sundialConfiguration;
}
```

## Create a Job Class

```java
public class SampleJob extends com.xeiam.sundial.Job {

  @Override
  public void doRun() throws JobInterruptException {
    // Do something interesting...
  }
}
```

##  ...with CronTrigger or SimpleTrigger Annotation
```java
@CronTrigger(cron = "0/5 * * * * ?")
```
```java
@SimpleTrigger(repeatInterval = 30, timeUnit = TimeUnit.SECONDS)
```

## Alternatively, Put an XML File Called jobs.xml on Classpath

```xml
<?xml version='1.0' encoding='utf-8'?>
<job-scheduling-data>

    <schedule>

        <!-- job with cron trigger -->
        <job>
            <name>SampleJob3</name>
            <job-class>com.foo.bar.jobs.SampleJob3</job-class>
            <concurrency-allowed>true</concurrency-allowed>
        </job>
        <trigger>
            <cron>
                <name>SampleJob3-Trigger</name>
                <job-name>SampleJob3</job-name>
                <cron-expression>*/15 * * * * ?</cron-expression>
            </cron>
        </trigger>

        <!-- job with simple trigger -->
        <job>
            <name>SampleJob2</name>
            <job-class>com.foo.bar.jobs.SampleJob2</job-class>
{'fraction_non_alphanumeric': 0.09081934846989141, 'fraction_numerical': 0.010576787477083627, 'mean_word_length': 3.8406143344709895, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 2, '>': 79, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '994990', 'n_tokens_mistral': 4250, 'n_tokens_neox': 3969, 'n_words': 1596}