1 /***************************************************************************************
2 * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3 * http://aspectwerkz.codehaus.org *
4 * ---------------------------------------------------------------------------------- *
5 * The software in this package is published under the terms of the LGPL license *
6 * a copy of which has been included with this distribution in the license.txt file. *
7 **************************************************************************************/
8 package org.codehaus.aspectwerkz;
9
10 import org.codehaus.aspectwerkz.definition.SystemDefinition;
11
12 /***
13 * Enum containing the different deployment model types. Used to be type-safe but that added to much overhead (0.00004
14 * ms/call) compared to the current implementation.
15 *
16 * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
17 */
18 public final class DeploymentModel {
19 public static final int PER_JVM = 0;
20
21 public static final int PER_CLASS = 1;
22
23 public static final int PER_INSTANCE = 2;
24
25 public static final int PER_THREAD = 3;
26
27 /***
28 * Converts the deployment model from string to int type.
29 *
30 * @param type the string type
31 * @return the matched deployment type
32 */
33 public static int getDeploymentModelAsInt(final String type) {
34 if ((type == null) || type.equals(SystemDefinition.PER_JVM)) {
35 return PER_JVM;
36 } else if (type.equals(SystemDefinition.PER_CLASS)) {
37 return PER_CLASS;
38 } else if (type.equals(SystemDefinition.PER_INSTANCE)) {
39 return PER_INSTANCE;
40 } else if (type.equals(SystemDefinition.PER_THREAD)) {
41 return PER_THREAD;
42 } else {
43 throw new RuntimeException("invalid deployment model: " + type);
44 }
45 }
46
47 /***
48 * Converts the deployment model from int to string type.
49 *
50 * @param type the int type
51 * @return the string type
52 */
53 public static String getDeploymentModelAsString(final int type) {
54 final String deploymentModel;
55 switch (type) {
56 case PER_JVM:
57 deploymentModel = SystemDefinition.PER_JVM;
58 break;
59 case PER_CLASS:
60 deploymentModel = SystemDefinition.PER_CLASS;
61 break;
62 case PER_INSTANCE:
63 deploymentModel = SystemDefinition.PER_INSTANCE;
64 break;
65 case PER_THREAD:
66 deploymentModel = SystemDefinition.PER_THREAD;
67 break;
68 default:
69 throw new IllegalArgumentException("no such deployment model type");
70 }
71 return deploymentModel;
72 }
73
74 /***
75 * Check mixin deployment model is compatible with aspect' ones Supported models are: Mixin Aspect perJVM perJVM
76 * perClass perJVM,perClass perInstance perJVM,perClass,perInstance perThread perThread
77 *
78 * @param mixinModel
79 * @param aspectModel
80 * @return true if compatible
81 */
82 public static boolean isMixinDeploymentModelCompatible(int mixinModel, int aspectModel) {
83
84 if (mixinModel == PER_THREAD) {
85 return (aspectModel == PER_THREAD);
86 } else {
87 return (mixinModel >= aspectModel);
88 }
89 }
90 }