clazz) {
73 | throw new UnsupportedOperationException();
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/impl/src/main/java/io/astefanutti/metrics/aspectj/JavaSeMetricStrategy.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
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 | package io.astefanutti.metrics.aspectj;
17 |
18 | import com.codahale.metrics.MetricRegistry;
19 | import com.codahale.metrics.SharedMetricRegistries;
20 |
21 | import java.util.regex.Matcher;
22 |
23 | /* package-private */ final class JavaSeMetricStrategy implements MetricStrategy {
24 |
25 | @Override
26 | public MetricRegistry resolveMetricRegistry(String registry) {
27 | Matcher matcher = EL_PATTERN.matcher(registry);
28 | if (matcher.find())
29 | throw new UnsupportedOperationException("Unsupported EL expression [" + registry + "] evaluation as no EL implementation is available");
30 | else
31 | return SharedMetricRegistries.getOrCreate(registry);
32 | }
33 |
34 | @Override
35 | public String resolveMetricName(String name) {
36 | Matcher matcher = EL_PATTERN.matcher(name);
37 | if (matcher.matches())
38 | throw new UnsupportedOperationException("Unsupported EL expression [" + name + "] evaluation as no EL implementation is available");
39 | else
40 | return name;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/impl/src/main/java/io/astefanutti/metrics/aspectj/JavaxElMetricStrategy.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
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 | package io.astefanutti.metrics.aspectj;
17 |
18 | import com.codahale.metrics.MetricRegistry;
19 | import com.codahale.metrics.SharedMetricRegistries;
20 |
21 | import javax.el.ELProcessor;
22 | import java.util.regex.Matcher;
23 |
24 | /* package-private */ final class JavaxElMetricStrategy implements MetricStrategy {
25 |
26 | private final ELProcessor processor;
27 |
28 | JavaxElMetricStrategy(Object object) {
29 | processor = new ELProcessor();
30 | processor.defineBean("this", object);
31 | }
32 |
33 | JavaxElMetricStrategy(Class> clazz) {
34 | processor = new ELProcessor();
35 | processor.getELManager().importClass(clazz.getName());
36 | }
37 |
38 | @Override
39 | public MetricRegistry resolveMetricRegistry(String registry) {
40 | Matcher matcher = EL_PATTERN.matcher(registry);
41 | if (matcher.matches()) {
42 | Object evaluation = processor.eval(matcher.group(1));
43 | if (evaluation instanceof String)
44 | return SharedMetricRegistries.getOrCreate((String) evaluation);
45 | else if (evaluation instanceof MetricRegistry)
46 | return (MetricRegistry) evaluation;
47 | else
48 | throw new IllegalStateException("Unable to resolve metrics registry from expression [" + registry + "]");
49 | } else if (!matcher.find()) {
50 | return SharedMetricRegistries.getOrCreate(registry);
51 | } else {
52 | return SharedMetricRegistries.getOrCreate(evaluateCompositeExpression(matcher));
53 | }
54 | }
55 |
56 | @Override
57 | public String resolveMetricName(String name) {
58 | Matcher matcher = EL_PATTERN.matcher(name);
59 | if (!matcher.find())
60 | return name;
61 | else
62 | return evaluateCompositeExpression(matcher);
63 | }
64 |
65 | private String evaluateCompositeExpression(Matcher matcher) {
66 | StringBuffer buffer = new StringBuffer();
67 | do {
68 | Object result = processor.eval(matcher.group(1));
69 | matcher.appendReplacement(buffer, result != null ? String.valueOf(result) : "");
70 | } while (matcher.find());
71 |
72 | matcher.appendTail(buffer);
73 | return buffer.toString();
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/impl/src/main/java/io/astefanutti/metrics/aspectj/MetricStrategy.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
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 | package io.astefanutti.metrics.aspectj;
17 |
18 | import com.codahale.metrics.MetricRegistry;
19 |
20 | import java.util.regex.Pattern;
21 |
22 | /* package-private */ interface MetricStrategy {
23 |
24 | Pattern EL_PATTERN = Pattern.compile("[#|$]\\{(.*)\\}");
25 |
26 | MetricRegistry resolveMetricRegistry(String registry);
27 |
28 | String resolveMetricName(String name);
29 | }
30 |
--------------------------------------------------------------------------------
/impl/src/main/java/io/astefanutti/metrics/aspectj/MetricStrategyFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
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 | package io.astefanutti.metrics.aspectj;
17 |
18 | /* package-private */ final class MetricStrategyFactory {
19 |
20 | static MetricStrategy newInstance(Object object) {
21 | if (isElAvailable(object.getClass()))
22 | return new JavaxElMetricStrategy(object);
23 | else
24 | // Expression Language 3.0 is not available, fall back to SE implementation
25 | return new JavaSeMetricStrategy();
26 | }
27 |
28 | static MetricStrategy newInstance(Class> clazz) {
29 | if (isElAvailable(clazz))
30 | return new JavaxElMetricStrategy(clazz);
31 | else
32 | // Expression Language 3.0 is not available, fall back to SE implementation
33 | return new JavaSeMetricStrategy();
34 | }
35 |
36 | private static ClassLoader getClassLoader(Class> clazz) {
37 | if (Thread.currentThread().getContextClassLoader() != null)
38 | return Thread.currentThread().getContextClassLoader();
39 | else
40 | return clazz.getClassLoader();
41 | }
42 |
43 | private static boolean isElAvailable(Class> clazz) {
44 | try {
45 | getClassLoader(clazz).loadClass("javax.el.ELProcessor");
46 | return true;
47 | } catch (ClassNotFoundException cause) {
48 | return false;
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/impl/src/main/java/io/astefanutti/metrics/aspectj/Metrics.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
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 | package io.astefanutti.metrics.aspectj;
17 |
18 | import java.lang.annotation.ElementType;
19 | import java.lang.annotation.Inherited;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.RetentionPolicy;
22 | import java.lang.annotation.Target;
23 |
24 | /**
25 | * An annotation that activates Metrics AspectJ weaving.
26 | *
27 | * Metrics AspectJ will scan all the declared methods of the annotated class that are annotated with
28 | * some Metrics annotations, then create and register the corresponding Metric instances and finally weave
29 | * its aspects around these methods, so that at runtime, these Metric instances get called according
30 | * to the Metrics annotations specification.
31 | *
32 | *
33 | * For example, given the following class declaration:
34 | *
35 | * {@literal @}Metrics(registry = "${this.registry}")
36 | * public class TimedMethodWithRegistryFromProperty {
37 | *
38 | * private final MetricRegistry registry;
39 | *
40 | * public TimedMethodWithRegistryFromProperty(MetricRegistry registry) {
41 | * this.registry = registry;
42 | * }
43 | *
44 | * public MetricRegistry getRegistry() {
45 | * return registry;
46 | * }
47 | *
48 | * {@literal @}Timed(name = "timerName")
49 | * public void timedMethod() {
50 | * }
51 | * }
52 | *
53 | * A {@code Timer} instance will be registered in the provided {@code MetricRegistry}.
54 | */
55 | @Inherited
56 | @Target({ElementType.TYPE})
57 | @Retention(RetentionPolicy.RUNTIME)
58 | public @interface Metrics {
59 |
60 | /**
61 | * The expression that is used to resolve the {@code MetricRegistry} to be used to register metrics into.
62 | *
63 | * Its value can either be:
64 | *
65 | * - a string literal that identifies a {@code MetricRegistry} accessible
66 | * from the {@code SharedMetricRegistries} class,
67 | * - or a valid EL expression that evaluates
68 | * to the registry name or the {@code MetricRegistry} instance.
69 | *
70 | *
71 | * The resultant {@code MetricRegistry} is used to register the {@code Metric} instances into.
72 | */
73 | String registry() default "metrics-registry";
74 |
75 | }
--------------------------------------------------------------------------------
/impl/src/main/java/io/astefanutti/metrics/aspectj/Profiled.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright © 2013 Antonin Stefanutti (antonin.stefanutti@gmail.com)
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 | package io.astefanutti.metrics.aspectj;
17 |
18 | /* package-private */ interface Profiled {
19 | }
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 |
6 | org.sonatype.oss
7 | oss-parent
8 | 9
9 |
10 |
11 | io.astefanutti.metrics.aspectj
12 | metrics-aspectj-parent
13 | 1.3-SNAPSHOT
14 | pom
15 |
16 | Metrics AspectJ Parent
17 | 2013
18 | AspectJ for Metrics
19 | https://github.com/astefanutti/metrics-aspectj
20 |
21 |
22 |
23 | Antonin Stefanutti
24 | antonin.stefanutti@gmail.com
25 | +1
26 |
27 |
28 |
29 |
30 |
31 | Apache License 2.0
32 | http://www.apache.org/licenses/LICENSE-2.0.html
33 |
34 |
35 |
36 |
37 | github
38 | https://github.com/astefanutti/metrics-aspectj/issues
39 |
40 |
41 |
42 | scm:git:git@github.com:astefanutti/metrics-aspectj.git
43 | scm:git:git@github.com:astefanutti/metrics-aspectj.git
44 | scm:git:git@github.com:astefanutti/metrics-aspectj.git
45 | HEAD
46 |
47 |
48 |
49 | UTF-8
50 | 3.2.2
51 | 1.8.10
52 |
53 |
54 |
55 | impl
56 | deps
57 | envs
58 |
59 |
60 |
61 | clean package
62 |
63 |
64 |
65 |
66 | com.mycila
67 | license-maven-plugin
68 | 3.0
69 |
70 |
71 |
72 | org.codehaus.mojo
73 | aspectj-maven-plugin
74 | 1.10
75 |
76 |
77 | org.aspectj
78 | aspectjtools
79 | ${aspectj.version}
80 |
81 |
82 |
83 |
84 |
85 | org.apache.maven.plugins
86 | maven-compiler-plugin
87 | 3.6.1
88 |
89 |
90 |
91 | org.apache.maven.plugins
92 | maven-jar-plugin
93 | 3.0.2
94 |
95 |
96 |
97 | org.apache.maven.plugins
98 | maven-shade-plugin
99 | 3.0.0
100 |
101 |
102 |
103 | org.apache.maven.plugins
104 | maven-surefire-plugin
105 | 2.19.1
106 |
107 |
108 |
109 | org.jacoco
110 | jacoco-maven-plugin
111 | 0.7.9
112 |
113 |
114 |
115 | org.eluder.coveralls
116 | coveralls-maven-plugin
117 | 4.3.0
118 |
119 |
120 | ${project.basedir}/impl/target/site/jacoco/jacoco.xml
121 |
122 | ${project.basedir}/impl/target/coveralls.json
123 |
124 | ${project.basedir}/impl/src/main/java
125 | ${project.basedir}/impl/src/main/aspect
126 |
127 |
128 |
129 |
130 |
131 | org.apache.maven.plugins
132 | maven-gpg-plugin
133 | 1.6
134 |
135 |
136 |
137 | org.apache.maven.plugins
138 | maven-release-plugin
139 | 2.5.3
140 |
141 | true
142 | forked-path
143 | false
144 | ${arguments} -Psonatype-oss-release
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 | com.mycila
153 | license-maven-plugin
154 |
155 | com/mycila/maven/plugin/license/templates/APACHE-2.txt
156 |
157 | Antonin Stefanutti
158 | antonin.stefanutti@gmail.com
159 | ${project.inceptionYear}
160 |
161 |
162 | src/**/*.java
163 | src/**/*.aj
164 |
165 |
166 |
167 |
168 | process-sources
169 |
170 | format
171 |
172 |
173 |
174 |
175 |
176 |
177 | org.apache.maven.plugins
178 | maven-compiler-plugin
179 |
180 | 1.6
181 | 1.6
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 | io.dropwizard.metrics
194 | metrics-core
195 | ${metrics.version}
196 |
197 |
198 |
199 | io.dropwizard.metrics
200 | metrics-annotation
201 | ${metrics.version}
202 |
203 |
204 | org.slf4j
205 | slf4j-api
206 |
207 |
208 |
209 |
210 |
211 | org.aspectj
212 | aspectjrt
213 | ${aspectj.version}
214 |
215 |
216 |
217 |
218 |
219 | javax.el
220 | javax.el-api
221 | 3.0.0
222 | provided
223 |
224 |
225 |
226 |
227 |
228 | org.glassfish
229 | javax.el
230 | 3.0.0
231 | test
232 |
233 |
234 |
235 | junit
236 | junit
237 | 4.13.1
238 | test
239 |
240 |
241 |
242 | org.hamcrest
243 | hamcrest-library
244 | 1.3
245 | test
246 |
247 |
248 |
249 | org.easytesting
250 | fest-reflect
251 | 1.4.1
252 | test
253 |
254 |
255 |
256 |
257 |
258 |
259 |
--------------------------------------------------------------------------------