alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (JPAPlugin.java)

This example Play Framework source code file (JPAPlugin.java) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

application, configuration, database, db, entitymanager, entitymanagerfactory, hashmap, jpa, jpaplugin, map, override, play, play framework, plugin, string

The JPAPlugin.java Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.db.jpa;

import play.*;

import java.io.*;
import java.util.*;

import javax.persistence.*;

/**
 * A Play plugin that automatically manages JPA configuration.
 */
public class JPAPlugin extends Plugin {
    
    private final Application application;
    
    public JPAPlugin(Application application) {
        this.application = application;
    }
    
    // --
    
    private final Map<String,EntityManagerFactory> emfs = new HashMap<String,EntityManagerFactory>();
    
    /**
     * Reads the configuration file and initialises required JPA EntityManagerFactories.
     */
    public void onStart() {

        Configuration jpaConf = Configuration.root().getConfig("jpa");
        
        if(jpaConf != null) {
            for(String key: jpaConf.keys()) {
                String persistenceUnit = jpaConf.getString(key);
                emfs.put(key, Persistence.createEntityManagerFactory(persistenceUnit));
            }
        }
        
    }
    
    private boolean isPluginDisabled() {
        String status =  application.configuration().getString("jpaplugin");
        return status != null && status.equals("disabled");
    }   

    @Override
    public boolean enabled() { 
        return isPluginDisabled() == false;
    }
     
    public void onStop() {
        
        for(EntityManagerFactory emf: emfs.values()) {
            emf.close();
        }
        
    }
    
    public EntityManager em(String key) {
        EntityManagerFactory emf = emfs.get(key);
        if(emf == null) {
            return null;
        }
        return emf.createEntityManager();
    }
    
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework JPAPlugin.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.