Skip to content

Commit

Permalink
Refactored code in Versions and error dialog if URL is not correct
Browse files Browse the repository at this point in the history
  • Loading branch information
maris123 committed Oct 31, 2017
1 parent 0978543 commit 62edf4b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class HydraCompilerConfigurable (project: Project) extends AbstractConfigurable(
form.selectedNoOfCores != settings.noOfCores ||
form.selectedSourcePartitioner != settings.sourcePartitioner ||
form.getHydraStoreDirectory != settings.hydraStorePath ||
form.getHydraRepository != hydraGlobalSettings.hydraRepositoryUrl ||
form.getHydraRepository != hydraGlobalSettings.getHydraRepositoryUrl.toString ||
form.getHydraRepositoryRealm != hydraGlobalSettings.hydraRepositoryRealm

override def reset() {
Expand All @@ -36,7 +36,7 @@ class HydraCompilerConfigurable (project: Project) extends AbstractConfigurable(
form.setSelectedVersion(settings.hydraVersion)
form.setSelectedSourcePartitioner(settings.sourcePartitioner)
form.setHydraStoreDirectory(settings.hydraStorePath)
form.setHydraRepository(hydraGlobalSettings.hydraRepositoryUrl)
form.setHydraRepository(hydraGlobalSettings.getHydraRepositoryUrl.toString)
form.setHydraRepositoryRealm(hydraGlobalSettings.hydraRepositoryRealm)
}

Expand All @@ -47,7 +47,8 @@ class HydraCompilerConfigurable (project: Project) extends AbstractConfigurable(
settings.noOfCores = form.selectedNoOfCores
settings.sourcePartitioner = form.selectedSourcePartitioner
settings.hydraStorePath = form.getHydraStoreDirectory
hydraGlobalSettings.hydraRepositoryUrl = form.getHydraRepository
hydraGlobalSettings.setHydraRepositopryUrl(form.getHydraRepository)
form.setHydraRepository(hydraGlobalSettings.getHydraRepositoryUrl.toString)
hydraGlobalSettings.hydraRepositoryRealm = form.getHydraRepositoryRealm
HydraCredentialsManager.setCredentials(form.getUsername, form.getPassword)
EditorNotifications.updateAll()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jetbrains.plugins.hydra.compiler

import java.awt.event.{ActionEvent, FocusEvent, FocusListener}
import java.net.URL
import javax.swing.event.DocumentEvent

import com.intellij.openapi.fileChooser.FileChooserDescriptor
Expand All @@ -12,7 +13,7 @@ import org.jetbrains.plugins.hydra.caches.HydraArtifactsCache
import org.jetbrains.plugins.hydra.settings.HydraApplicationSettings
import org.jetbrains.plugins.scala.extensions

import scala.util.{Failure, Success}
import scala.util.{Failure, Success, Try}

/**
* @author Maris Alexandru
Expand All @@ -23,7 +24,7 @@ class ScalaHydraCompilerConfigurationPanel(project: Project, settings: HydraComp

private val documentAdapter = new DocumentAdapter {
override def textChanged(documentEvent: DocumentEvent): Unit =
downloadButton.setEnabled(getUsername.nonEmpty && getPassword.nonEmpty && getHydraRepository.nonEmpty)
downloadButton.setEnabled(getUsername.nonEmpty && getPassword.nonEmpty && getHydraRepository.nonEmpty && getHydraRepositoryRealm.nonEmpty)
}

private val focusListener = new FocusListener {
Expand All @@ -32,21 +33,36 @@ class ScalaHydraCompilerConfigurationPanel(project: Project, settings: HydraComp
override def focusLost(e: FocusEvent): Unit = if (getUsername.nonEmpty && getPassword.nonEmpty &&
(HydraCredentialsManager.getLogin != getUsername ||
HydraCredentialsManager.getPlainPassword != getPassword ||
hydraGlobalSettings.hydraRepositoryUrl != getHydraRepository ||
hydraGlobalSettings.hydraRepositoryRealm != getHydraRepositoryRealm)) {

HydraCredentialsManager.setCredentials(getUsername, getPassword)
hydraGlobalSettings.hydraRepositoryUrl = getHydraRepository
hydraVersionComboBox.setItems(HydraVersions.downloadHydraVersions)
hydraVersionComboBox.setSelectedItem(settings.hydraVersion)
}
}

private val urlFocusListener = new FocusListener {
override def focusGained(e: FocusEvent): Unit = {}

override def focusLost(e: FocusEvent): Unit = if (hydraGlobalSettings.getHydraRepositoryUrl.toString != getHydraRepository) {
Try(new URL(getHydraRepository)) match {
case Success(_) => {
hydraGlobalSettings.setHydraRepositopryUrl(getHydraRepository)
setHydraRepository(hydraGlobalSettings.getHydraRepositoryUrl.toString)
hydraVersionComboBox.setItems(HydraVersions.downloadHydraVersions)
hydraVersionComboBox.setSelectedItem(settings.hydraVersion)
}
case Failure(_) => Messages.showErrorDialog(contentPanel, s"$getHydraRepository is not valid. Default URL will be used.", "URL not valid")
}

}
}

hydraGlobalSettings.getState
setDefaultHydraVersion

hydraRepository.setText(hydraGlobalSettings.hydraRepositoryUrl)
hydraRepository.addFocusListener(focusListener)
hydraRepository.setText(hydraGlobalSettings.getHydraRepositoryUrl.toString)
hydraRepository.addFocusListener(urlFocusListener)
hydraRepository.getDocument.addDocumentListener(documentAdapter)

realmTextField.setText(hydraGlobalSettings.hydraRepositoryRealm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.intellij.openapi.components._

import scala.beans.BeanProperty
import scala.collection.JavaConverters._
import scala.util.Try

/**
* @author Maris Alexandru
Expand All @@ -19,22 +20,24 @@ import scala.collection.JavaConverters._
class HydraApplicationSettings extends PersistentStateComponent[HydraApplicationSettingsState]{

var artifactPaths: Map[(String, String), List[String]] = Map.empty
var hydraRepositoryUrl: String = HydraApplicationSettings.DefaultHydraRepositoryUrl
private var hydraRepositoryUrl: URL = HydraApplicationSettings.DefaultHydraRepositoryUrl
var hydraRepositoryRealm: String = HydraApplicationSettings.DefaultHydraRepositoryRealm
private val KeySeparator = "_"

override def loadState(state: HydraApplicationSettingsState): Unit = {
state.removeMapEntriesThatDontExist()
artifactPaths = convertArtifactsFromStateToSettings(state.getGlobalArtifactPaths)
hydraRepositoryUrl = state.getHydraRepositoryUrl
hydraRepositoryUrl = parseUrl(state.getHydraRepositoryUrl)
hydraRepositoryRealm = state.hydraRepositoryRealm
}

override def getState: HydraApplicationSettingsState = {
val state = new HydraApplicationSettingsState
val artifacts = artifactPaths map { case((scalaVer, hydraVer), value) => (scalaVer + KeySeparator + hydraVer, value.asJava)}
state.setGlobalArtifactPaths(artifacts.asJava)
state.removeMapEntriesThatDontExist()
state.setHydraRepositoryUrl(hydraRepositoryUrl)
state.setHydraRepositoryUrl(hydraRepositoryUrl.toString)
state.setHydraRepositoryRealm(hydraRepositoryRealm)
artifactPaths = convertArtifactsFromStateToSettings(state.getGlobalArtifactPaths)
state
}
Expand All @@ -47,9 +50,13 @@ class HydraApplicationSettings extends PersistentStateComponent[HydraApplication
for { (scalaVersion, _) <- artifactPaths.keySet.toArray } yield scalaVersion
}

def getHydraRepositoryName: String = new URL(HydraApplicationSettings.getInstance().hydraRepositoryUrl).getHost
def getHydraRepositoryName: String = hydraRepositoryUrl.getHost

def getHydraRepositoryUrl: String = if(hydraRepositoryUrl.endsWith("/")) hydraRepositoryUrl.dropRight(1) else hydraRepositoryUrl
def setHydraRepositopryUrl(url: String): Unit = hydraRepositoryUrl = parseUrl(url)

def getHydraRepositoryUrl: URL = hydraRepositoryUrl

private def parseUrl(url: String) = Try(new URL(if (url.endsWith("/")) url else url.concat("/"))) getOrElse HydraApplicationSettings.DefaultHydraRepositoryUrl

private def convertArtifactsFromStateToSettings(artifacts: java.util.Map[String, java.util.List[String]]) = {
val resultArtifacts = for {
Expand All @@ -67,7 +74,7 @@ class HydraApplicationSettingsState {
var globalArtifactPaths: java.util.Map[String, java.util.List[String]] = new java.util.HashMap()

@BeanProperty
var hydraRepositoryUrl: String = HydraApplicationSettings.DefaultHydraRepositoryUrl
var hydraRepositoryUrl: String = HydraApplicationSettings.DefaultHydraRepositoryUrl.toString

@BeanProperty
var hydraRepositoryRealm: String = HydraApplicationSettings.DefaultHydraRepositoryRealm
Expand All @@ -82,8 +89,7 @@ class HydraApplicationSettingsState {
}

object HydraApplicationSettings {
val DefaultHydraRepositoryName = "repo.triplequote.com"
val DefaultHydraRepositoryUrl = s"https://$DefaultHydraRepositoryName/artifactory"
val DefaultHydraRepositoryUrl = new URL("https://repo.triplequote.com/artifactory/")
val DefaultHydraRepositoryRealm = "Artifactory Realm"
def getInstance(): HydraApplicationSettings = ServiceManager.getService(classOf[HydraApplicationSettings])
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jetbrains.plugins.scala.project

import java.net.{HttpURLConnection, URL}

import com.intellij.util.net.HttpConfigurable
import org.jetbrains.plugins.hydra.compiler.HydraCredentialsManager
import org.jetbrains.plugins.hydra.settings.HydraApplicationSettings
Expand Down Expand Up @@ -53,11 +55,15 @@ object Versions {
loadLinesFrom(url).map { lines => lines.collect(filter) }
}

private def loadLinesFrom(url: String): Try[Seq[String]] = {
private def loadHydraVersionsFrom(url: String, filter: PartialFunction[String, String]): Try[Seq[String]] = {
val loadedLines = loadLinesFrom(url){ connection => connection.setRequestProperty("Authorization", "Basic " + HydraCredentialsManager.getBasicAuthEncoding()) }
loadedLines.map { lines => lines.collect(filter) }
}

private def loadLinesFrom(url: String)(implicit prepareConnection: HttpURLConnection => Unit = _ => ()): Try[Seq[String]] = {
Try(HttpConfigurable.getInstance().openHttpConnection(url)).map { connection =>
try {
if(url.contains(HydraApplicationSettings.getInstance().getHydraRepositoryUrl))
connection.setRequestProperty("Authorization", "Basic " + HydraCredentialsManager.getBasicAuthEncoding())
prepareConnection(connection)
Source.fromInputStream(connection.getInputStream).getLines().toVector
} finally {
connection.disconnect()
Expand All @@ -67,13 +73,12 @@ object Versions {

private def loadVersionsForHydra() = {
val entity = Entity.Hydra
val repoUrl = HydraApplicationSettings.getInstance().getHydraRepositoryUrl + entity.url

val repoUrl = new URL(HydraApplicationSettings.getInstance().getHydraRepositoryUrl, entity.url).toString
def downloadHydraVersions(url: String): Seq[String] =
loadVersionsFrom(url, { case entity.pattern(number) => number }).getOrElse(entity.hardcodedVersions).map(Version(_))
loadHydraVersionsFrom(url, { case entity.pattern(number) => number }).getOrElse(entity.hardcodedVersions).map(Version(_))
.filter(_ >= entity.minVersion).map(_.presentation)

loadVersionsFrom(repoUrl, {
loadHydraVersionsFrom(repoUrl, {
case entity.pattern(number) => number
}).map { versions =>
versions.flatMap(version => downloadHydraVersions(s"""$repoUrl$version/""")).distinct
Expand Down Expand Up @@ -105,7 +110,7 @@ object Versions {
Version("0.2.0"),
Seq("0.2.0-RC1"))

val Hydra = Entity("/ivy-releases/com.triplequote/",
val Hydra = Entity("ivy-releases/com.triplequote/",
".+>(.*\\d+\\.\\d+\\.\\d+.*)/<.*".r,
Version("0.9.5"),
Seq("0.9.5")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ object Downloader {
private def sbtCommandsForHydra(platform: Platform, version: String) = {
val settings = HydraApplicationSettings.getInstance()
val repositoryName = settings.getHydraRepositoryName
val repositoryUrl = settings.getHydraRepositoryUrl
val repositoryUrl = settings.getHydraRepositoryUrl.toString
Seq(
s"""set scalaVersion := "${version.split("_")(0)}"""",
s"""set credentials := Seq(Credentials("${settings.hydraRepositoryRealm}", "${repositoryName}", "${HydraCredentialsManager.getLogin}", "${HydraCredentialsManager.getPlainPassword}"))""",
Expand Down

0 comments on commit 62edf4b

Please sign in to comment.