Skip to content

Commit

Permalink
ESC-17 Add support for Play 2 applications to persist to database
Browse files Browse the repository at this point in the history
* This is not using application data sources yet, more work needed.
* Try to use more for comprehensions to cleaner code base and avoid
touching Option methods directly, and code in a more monadic style.
* Since Play! applications remove logging handlers when starting up
(great!), add some logic to cache current log handlers when starting
Play applications and restore them right after.
* Switch to Dev mode for applications so that multiple applications
can be deployed one after the one and avoid previous application
configuration to be cached.
  • Loading branch information
galderz committed Apr 14, 2013
1 parent 4fe165b commit 78ddf41
Show file tree
Hide file tree
Showing 46 changed files with 1,258 additions and 527 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ logger.org.level=INFO
# play
logger.play.level=TRACE

## org.jboss.modules
#logger.org.jboss.modules.level=TRACE
# org.jboss.modules
# logger.org.jboss.modules.level=DEBUG

# Console handler configuration
handler.CONSOLE=org.jboss.logmanager.handlers.ConsoleHandler
Expand All @@ -41,7 +41,7 @@ handler.CONSOLE.formatter=PATTERN

# File handler configuration
handler.FILE=org.jboss.logmanager.handlers.FileHandler
handler.FILE.level=DEBUG
handler.FILE.level=TRACE
handler.FILE.properties=autoFlush,fileName
handler.FILE.autoFlush=true
handler.FILE.fileName=./target/test.log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package io.escalante.artifact

import maven.MavenArtifact
import org.jboss.as.server.deployment.DeploymentUnit
import scala.xml.Elem
import io.escalante.server.JBossModule

Expand All @@ -19,14 +18,6 @@ import io.escalante.server.JBossModule
*/
trait ArtifactRepository {

/**
* resolve and add as attachment to deployment unit in mount point
*/
def attachArtifacts(
artifacts: Seq[MavenArtifact],
deployment: DeploymentUnit,
mountPoint: String)

/**
* Install a Maven artifact in the artifact repository, returning a module
* metadata represented as an instance of JBossModule.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,131 +23,143 @@ import org.apache.maven.settings.building.{DefaultSettingsBuilderFactory, Defaul
*/
class MavenSettings {

import MavenSettings._
import MavenSettings._

val useMavenCentral = true
val useMavenCentral = true

val settings: Settings = buildDefaultSettings
val settings: Settings = buildDefaultSettings

def getRemoteRepositories: Seq[RemoteRepository] = {
// Disable repositories if working offline
if (settings.isOffline)
return Nil
def getRemoteRepositories: Seq[RemoteRepository] = {
// Disable repositories if working offline
if (settings.isOffline)
return Nil

val actives = settings.getActiveProfiles
val actives = settings.getActiveProfiles

val enhancedRepos = new ListBuffer[RemoteRepository]()
val enhancedRepos = new ListBuffer[RemoteRepository]()

asScalaIterator(settings.getProfilesAsMap.iterator).foreach {
case (profileName, profile) =>
val activation = profile.getActivation
if (actives.contains(profileName) || (activation != null
&& activation.isActiveByDefault)) {
asScalaIterator(profile.getRepositories.iterator()).foreach(
repo => enhancedRepos += asRemoteRepository(repo))
}
}
asScalaIterator(settings.getProfilesAsMap.iterator).foreach {
case (profileName, profile) =>
val activation = profile.getActivation
if (actives.contains(profileName) || (activation != null
&& activation.isActiveByDefault)) {
asScalaIterator(profile.getRepositories.iterator()).foreach(
repo => enhancedRepos += asRemoteRepository(repo))
}
}

// Repositories from other model pom files could be loaded here...
// Repositories from other model pom files could be loaded here...

if (useMavenCentral)
enhancedRepos += MAVEN_CENTRAL
if (useMavenCentral)
enhancedRepos += MAVEN_CENTRAL

if (settings.getMirrors.size() == 0)
return enhancedRepos ++ List()
// Add Typesafe repo
enhancedRepos += TYPESAFE

// Use mirrors if any to do the mirroring stuff
val dms = new DefaultMirrorSelector()
// Fill in mirrors
asScalaIterator(settings.getMirrors.iterator()).foreach { mirror =>
// Repository manager flag is set to false
// Maven does not support specifying it in the settings.xml
dms.add(mirror.getId, mirror.getUrl, mirror.getLayout,
false, mirror.getMirrorOf, mirror.getMirrorOfLayouts)
}
if (settings.getMirrors.size() == 0)
return enhancedRepos ++ List()

val mirrors = enhancedRepos.map { repo =>
val mirror = dms.getMirror(repo)
if (mirror != null) mirror else repo
}
// Use mirrors if any to do the mirroring stuff
val dms = new DefaultMirrorSelector()
// Fill in mirrors
asScalaIterator(settings.getMirrors.iterator()).foreach {
mirror =>
// Repository manager flag is set to false
// Maven does not support specifying it in the settings.xml
dms.add(mirror.getId, mirror.getUrl, mirror.getLayout,
false, mirror.getMirrorOf, mirror.getMirrorOfLayouts)
}

mirrors ++ List()
}
val mirrors = enhancedRepos.map {
repo =>
val mirror = dms.getMirror(repo)
if (mirror != null) mirror else repo
}

mirrors ++ List()
}

}

object MavenSettings {

private val MAVEN_CENTRAL = new RemoteRepository("central", "default",
"http://repo1.maven.org/maven2")
// TODO: Externalize repositories to artifact module configuration

private val MAVEN_CENTRAL = new RemoteRepository(
"central", "default", "http://repo1.maven.org/maven2")

private val TYPESAFE = new RemoteRepository(
"typesafe", "default", "http://repo.typesafe.com/typesafe/maven-releases")

private val ALT_USER_SETTINGS_XML_LOCATION = "org.apache.maven.user-settings"
private val ALT_USER_SETTINGS_XML_LOCATION = "org.apache.maven.user-settings"

private val ALT_GLOBAL_SETTINGS_XML_LOCATION = "org.apache.maven.global-settings"
private val ALT_GLOBAL_SETTINGS_XML_LOCATION = "org.apache.maven.global-settings"

private val DEFAULT_USER_SETTINGS_PATH = SecurityActions.getSystemProperty("user.home")
.concat("/.m2/settings.xml")
private val DEFAULT_USER_SETTINGS_PATH = SecurityActions.getSystemProperty("user.home")
.concat("/.m2/settings.xml")

private val ALT_LOCAL_REPOSITORY_LOCATION = "maven.repo.local"
private val ALT_LOCAL_REPOSITORY_LOCATION = "maven.repo.local"

private val DEFAULT_REPOSITORY_PATH = SecurityActions
.getSystemProperty("user.home").concat("/.m2/repository")
private val DEFAULT_REPOSITORY_PATH = SecurityActions
.getSystemProperty("user.home").concat("/.m2/repository")

private val ALT_MAVEN_OFFLINE = "org.apache.maven.offline"
private val ALT_MAVEN_OFFLINE = "org.apache.maven.offline"

private def buildDefaultSettings: Settings = {
val request = new DefaultSettingsBuildingRequest()
val altUserSettings = SecurityActions.getSystemProperty(
ALT_USER_SETTINGS_XML_LOCATION)
val altGlobalSettings = SecurityActions.getSystemProperty(
ALT_GLOBAL_SETTINGS_XML_LOCATION)
private def buildDefaultSettings: Settings = {
val request = new DefaultSettingsBuildingRequest()
val altUserSettings = SecurityActions.getSystemProperty(
ALT_USER_SETTINGS_XML_LOCATION)
val altGlobalSettings = SecurityActions.getSystemProperty(
ALT_GLOBAL_SETTINGS_XML_LOCATION)

request.setUserSettingsFile(new File(DEFAULT_USER_SETTINGS_PATH))
request.setUserSettingsFile(new File(DEFAULT_USER_SETTINGS_PATH))

// set alternate files
if (altUserSettings != null && altUserSettings.length() > 0)
request.setUserSettingsFile(new File(altUserSettings))
// set alternate files
if (altUserSettings != null && altUserSettings.length() > 0)
request.setUserSettingsFile(new File(altUserSettings))

if (altGlobalSettings != null && altGlobalSettings.length() > 0)
request.setGlobalSettingsFile(new File(altGlobalSettings))
if (altGlobalSettings != null && altGlobalSettings.length() > 0)
request.setGlobalSettingsFile(new File(altGlobalSettings))

val result = new DefaultSettingsBuilderFactory().newInstance().build(request)
val result = new DefaultSettingsBuilderFactory().newInstance().build(request)

val settings = result.getEffectiveSettings
val settings = result.getEffectiveSettings

// enrich with local repository
if (settings.getLocalRepository == null) {
val altLocalRepo = SecurityActions.getSystemProperty(ALT_LOCAL_REPOSITORY_LOCATION)
settings.setLocalRepository(DEFAULT_REPOSITORY_PATH)
// enrich with local repository
if (settings.getLocalRepository == null) {
val altLocalRepo = SecurityActions.getSystemProperty(ALT_LOCAL_REPOSITORY_LOCATION)
settings.setLocalRepository(DEFAULT_REPOSITORY_PATH)

if (altLocalRepo != null && altLocalRepo.length() > 0)
settings.setLocalRepository(altLocalRepo)
}
if (altLocalRepo != null && altLocalRepo.length() > 0)
settings.setLocalRepository(altLocalRepo)
}

val goOffline = SecurityActions.getSystemProperty(ALT_MAVEN_OFFLINE)
if (goOffline != null)
settings.setOffline(goOffline.toBoolean)
val goOffline = SecurityActions.getSystemProperty(ALT_MAVEN_OFFLINE)
if (goOffline != null)
settings.setOffline(goOffline.toBoolean)

settings
}
settings
}

private def asRemoteRepository(repository: MavenRepository): RemoteRepository = {
new RemoteRepository().setId(repository.getId).setContentType(repository.getLayout)
.setUrl(repository.getUrl).setPolicy(true, asRepositoryPolicy(repository.getSnapshots))
.setPolicy(false, asRepositoryPolicy(repository.getReleases))
}
private def asRemoteRepository(repository: MavenRepository): RemoteRepository = {
new RemoteRepository().setId(repository.getId).setContentType(repository.getLayout)
.setUrl(repository.getUrl).setPolicy(true, asRepositoryPolicy(repository.getSnapshots))
.setPolicy(false, asRepositoryPolicy(repository.getReleases))
}

private def asRepositoryPolicy(policy: MavenRepositoryPolicy): RepositoryPolicy = {
if (policy != null) {
new RepositoryPolicy(policy.isEnabled,
if (policy.getUpdatePolicy != null)
policy.getUpdatePolicy else RepositoryPolicy.UPDATE_POLICY_DAILY,
if (policy.getChecksumPolicy != null)
policy.getChecksumPolicy else RepositoryPolicy.CHECKSUM_POLICY_WARN)
} else {
new RepositoryPolicy(true, RepositoryPolicy.UPDATE_POLICY_DAILY,
RepositoryPolicy.CHECKSUM_POLICY_WARN)
}
}
private def asRepositoryPolicy(policy: MavenRepositoryPolicy): RepositoryPolicy = {
if (policy != null) {
new RepositoryPolicy(policy.isEnabled,
if (policy.getUpdatePolicy != null)
policy.getUpdatePolicy
else RepositoryPolicy.UPDATE_POLICY_DAILY,
if (policy.getChecksumPolicy != null)
policy.getChecksumPolicy
else RepositoryPolicy.CHECKSUM_POLICY_WARN)
} else {
new RepositoryPolicy(true, RepositoryPolicy.UPDATE_POLICY_DAILY,
RepositoryPolicy.CHECKSUM_POLICY_WARN)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.jboss.msc.service.{ServiceName, StartContext, StopContext, Service}
import io.escalante.logging.Log
import io.escalante.artifact.{ArtifactRepository}
import io.escalante.artifact.maven.MavenArtifact
import org.jboss.as.server.deployment.DeploymentUnit
import scala.xml.Elem
import org.jboss.msc.value.InjectedValue
import org.jboss.as.controller.services.path.PathManager
Expand Down Expand Up @@ -88,13 +87,6 @@ class ArtifactRepositoryService(
repository.installArtifact(artifact, dependencies, subArtifacts)
}

def attachArtifacts(
artifacts: Seq[MavenArtifact],
deployment: DeploymentUnit,
mountPoint: String) {
repository.attachArtifacts(artifacts, deployment, mountPoint)
}

}

object ArtifactRepositoryService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package io.escalante.server

import java.io.File
import org.jboss.as.server.deployment.DeploymentUnit
import io.escalante.artifact.maven.{MavenArtifact, MavenDependencyResolver}
import io.escalante.io.FileSystem._
import io.escalante.xml.ScalaXmlParser._
Expand Down Expand Up @@ -35,24 +34,6 @@ class AppServerRepository(root: File) extends ArtifactRepository with Log {
subArtifacts: Seq[MavenArtifact]): JBossModule =
installArtifact(artifact, None, dependencies, subArtifacts)

def attachArtifacts(
artifacts: Seq[MavenArtifact],
deployment: DeploymentUnit,
mountPoint: String) {
// TODO: Parallelize with Scala 2.10 futures...
// Flat map so that each maven dependencies files are then combined into
// a single sequence of files to add to deployment unit
val jars = artifacts.flatMap { artifact =>
MavenDependencyResolver.resolveArtifact(artifact)
// TODO: add more clever logic to resolveArtifact:
// - if lift 2.4 + scala 2.9.2 does not exist, check is scala version is latest
// - if it is, try "decreasing version", so "2.9.1"... that way all the way down
// - if it's not latest, try latest and then others
}.distinct // Remove duplicates to avoid duplicate mount errors

Deployments.attachTo(deployment, mountPoint, jars :_*)
}

private def installArtifact(
artifact: MavenArtifact,
moduleXml: Option[Elem],
Expand Down
Loading

0 comments on commit 78ddf41

Please sign in to comment.