Skip to content
This repository was archived by the owner on Aug 17, 2019. It is now read-only.

Commit 2275a49

Browse files
committed
Now provide info for the resource resolver
1 parent 2158a36 commit 2275a49

8 files changed

+97
-14
lines changed

src/main/scala/com/ckkloverdos/resource/ClassLoaderStreamResourceContext.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ final class ClassLoaderStreamResourceContext private(
3636

3737
def /(child: String) = new ClassLoaderStreamResourceContext(cl, parent, concatResourcePaths(this.extraPath, child))
3838

39-
def getLocalResource(path: String) = {
39+
def getLocalResourceX(path: String) = {
4040
val actualPath = concatResourcePaths(extraPath, path)
4141
logger.debug("Searching for local resource %s (actual: %s) in %s".format(path, actualPath, this))
4242
cl.getResource(actualPath) match {
4343
case null => NoVal
44-
case localURL => Maybe(new URLStreamResource(actualPath, localURL))
44+
case localURL => Maybe(ResolvedStreamResource(new URLStreamResource(actualPath, localURL), this))
4545
}
4646
}
4747

src/main/scala/com/ckkloverdos/resource/CompositeStreamResourceContext.scala

+17-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,23 @@ final class CompositeStreamResourceContext(
3030

3131
def /(child: String) = new CompositeStreamResourceContext(parent, others.map(_./(child)): _*)
3232

33-
def getLocalResource(path: String): Maybe[StreamResource] =
34-
others find { rc => rc.getResource(path).isJust } map { _.getResource(path) } getOrElse NoVal
33+
def getLocalResourceX(path: String): Maybe[ResolvedStreamResource] = {
34+
var _rrcM: Maybe[ResolvedStreamResource] = NoVal
35+
var _ctx: StreamResourceContext = null
36+
var _found = false
37+
val iter = others.iterator
38+
while(!_found && iter.hasNext) {
39+
_ctx = iter.next()
40+
_rrcM = _ctx.getResourceX(path)
41+
_found = _rrcM.isJust
42+
}
43+
44+
if(_found) {
45+
_rrcM
46+
} else {
47+
NoVal
48+
}
49+
}
3550

3651
override def toString = "CompositeStreamResourceContext(%s, %s)".format(parent, others)
3752
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2011 Christos KK Loverdos
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.ckkloverdos.resource
18+
19+
import com.ckkloverdos.maybe.NoVal
20+
21+
/**
22+
* A resource context that provides nothing.
23+
*
24+
* @author Christos KK Loverdos <loverdos@gmail.com>.
25+
*/
26+
object EmptyStreamResourceContext extends StreamResourceContextSkeleton(NoVal) {
27+
def /(child: String) = this
28+
29+
def getLocalResourceX(path: String) = NoVal
30+
}

src/main/scala/com/ckkloverdos/resource/FileStreamResourceContext.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ final class FileStreamResourceContext(
5050

5151
def /(child: String) = new FileStreamResourceContext(new File(this.root, child), parent.map(_./(child)))
5252

53-
def getLocalResource(path: String) = {
53+
def getLocalResourceX(path: String) = {
5454
new File(root, path) match {
55-
case file if file.exists => Maybe(new FileStreamResource(file))
55+
case file if file.exists => Maybe(ResolvedStreamResource(new FileStreamResource(file), this))
5656
case _ => NoVal
5757
}
5858
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2012 Christos KK Loverdos
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.ckkloverdos.resource
18+
19+
/**
20+
* A [[com.ckkloverdos.resource.StreamResource]] with the [[com.ckkloverdos.resource.StreamResourceContext]] that
21+
* resolved it.
22+
*
23+
* @author Christos KK Loverdos <loverdos@gmail.com>
24+
*/
25+
26+
case class ResolvedStreamResource(resource: StreamResource, resolver: StreamResourceContext)

src/main/scala/com/ckkloverdos/resource/StreamResourceContext.scala

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ trait StreamResourceContext {
3232
def getResource(path: String): Maybe[StreamResource]
3333

3434
def getLocalResource(path: String): Maybe[StreamResource]
35+
36+
def getResourceX(path: String): Maybe[ResolvedStreamResource]
37+
38+
def getLocalResourceX(path: String): Maybe[ResolvedStreamResource]
3539
}
3640

3741
object StreamResourceContext {

src/main/scala/com/ckkloverdos/resource/StreamResourceContextSkeleton.scala

+14-6
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,27 @@ abstract class StreamResourceContextSkeleton(_parent: Maybe[StreamResourceContex
2929
def parent = _parent
3030

3131
def getResource(path: String) = {
32-
getLocalResource(path) match {
33-
case j@Just(rc) =>
34-
logger.debug(" Found %s".format(rc))
32+
getResourceX(path).map(_.resource)
33+
}
34+
35+
def getLocalResource(path: String) = {
36+
getLocalResourceX(path).map(_.resource)
37+
}
38+
39+
def getResourceX(path: String) = {
40+
getLocalResourceX(path) match {
41+
case j@Just(rrc)
42+
logger.debug(" Found %s".format(rrc))
3543
j
36-
case NoVal =>
44+
case NoVal
3745
logger.debug(" ==> Not found")
3846
if(parent.isJust) {
3947
logger.debug(" Trying parent %s".format(parent))
40-
parent.flatMap(_.getResource(path))
48+
parent.flatMap(_.getResourceX(path))
4149
} else {
4250
NoVal
4351
}
44-
case f@Failed(_, _) =>
52+
case f@Failed(_, _)
4553
logger.warn("Error %s".format(f))
4654
f
4755
}

src/test/scala/com/ckkloverdos/resource/ResourceTest.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ class ResourceTest {
131131

132132
@Test
133133
def testPropsBoolean {
134-
val key1val = ("key1", "on", true)
135-
val key2val = ("key2", "off", false)
134+
val key1val = ("key1", "true", true)
135+
val key2val = ("key2", "false", false)
136136
val key3val = ("key3", "0", false)
137137
val all = Seq(key1val, key2val, key3val)
138138
val keyvals = all map { case (k, v, _) => (k, v) }

0 commit comments

Comments
 (0)