1 package com.opensymphony.xwork2.config.impl;
2
3 import com.opensymphony.xwork2.inject.Context;
4 import com.opensymphony.xwork2.inject.Factory;
5 import com.opensymphony.xwork2.inject.Scope;
6 import com.opensymphony.xwork2.util.location.Located;
7 import com.opensymphony.xwork2.util.location.LocationUtils;
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 @Override
37 public String toString() {
38 String fields = new LinkedHashMap<String, Object>() {
39 {
40 put("type", type);
41 put("name", name);
42 put("implementation", implementation);
43 put("scope", scope);
44 }
45 }.toString();
46 StringBuilder sb = new StringBuilder(fields);
47 sb.append(super.toString());
48 sb.append(" defined at ");
49 sb.append(getLocation().toString());
50 return sb.toString();
51 }
52 }