1 package com.opensymphony.xwork2.config.impl;
2
3 import com.opensymphony.xwork2.util.location.Located;
4 import com.opensymphony.xwork2.util.location.LocationUtils;
5 import com.opensymphony.xwork2.inject.Factory;
6 import com.opensymphony.xwork2.inject.Scope;
7 import com.opensymphony.xwork2.inject.Context;
8
9 import java.util.LinkedHashMap;
10
11 /**
12 * Attaches location information to the factory.
13 */
14 public class LocatableFactory<T> extends Located implements Factory<T> {
15
16
17 private Class implementation;
18 private Class type;
19 private String name;
20 private Scope scope;
21
22 public LocatableFactory(String name, Class type, Class implementation, Scope scope, Object location) {
23 this.implementation = implementation;
24 this.type = type;
25 this.name = name;
26 this.scope = scope;
27 setLocation(LocationUtils.getLocation(location));
28 }
29
30 @SuppressWarnings("unchecked")
31 public T create(Context context) {
32 Object obj = context.getContainer().inject(implementation);
33 return (T) obj;
34 }
35
36 public String toString() {
37 String fields = new LinkedHashMap<String, Object>() {
38 {
39 put("type", type);
40 put("name", name);
41 put("implementation", implementation);
42 put("scope", scope);
43 }
44 }.toString();
45 StringBuilder sb = new StringBuilder(fields);
46 sb.append(super.toString());
47 sb.append(" defined at ");
48 sb.append(getLocation().toString());
49 return sb.toString();
50 }
51 }