@Override public Context apply(View input) { return input.unwrap(); } };
@Override public Context apply(View input) { return input.unwrap(); } };
public void testAssignability() { View view = ContextBuilder.newBuilder(new EC2ApiMetadata()).credentials("foo", "bar") .buildView(typeToken(ComputeServiceContext.class)); view.unwrapApi(EC2Api.class); } }
@Override public Context apply(View input) { return input.unwrap(); } };
public void testAssignability() { View view = ContextBuilder.newBuilder(new CloudStackApiMetadata()).credentials("foo", "bar") .buildView(typeToken(ComputeServiceContext.class)); view.unwrapApi(CloudStackApi.class); } }
@Override public Context apply(@Nullable View input) { return input.unwrap(); } });
/** * {@inheritDoc} */ @Override public Context getContext(String name) { return views.get(name).unwrap(); }
/** * {@inheritDoc} */ @Override public <V extends View> void register(V view) { views.put(view.unwrap().getName(), view); }
/** * {@inheritDoc} */ @Override public <V extends View> void unregister(V view) { views.remove(view.unwrap().getName()); }
/** * Extracts the {@link Context}. * * @param service * @param <T> * @return */ public static <T> Context toContext(T service) { Context ctx = null; Class c = service.getClass(); try { //Ugly way to get the Context, but there doesn't seem to be a better one. Method m = c.getMethod("getContext"); Object obj = m.invoke(service); if (Context.class.isAssignableFrom(obj.getClass())) { ctx = (Context) obj; } else if (View.class.isAssignableFrom(obj.getClass())) { ctx = ((View) obj).unwrap(); } else { throw new IllegalArgumentException("Service doesn't have a context."); } } catch (NoSuchMethodException e) { throw new IllegalArgumentException(e); } catch (InvocationTargetException e) { throw new IllegalArgumentException(e); } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } return ctx; }
/** * Extracts the {@link Context}. * * @param service * @param <T> * @return */ public static <T> Context toContext(T service) { Context ctx = null; Class c = service.getClass(); try { //Ugly way to get the Context, but there doesn't seem to be a better one. Method m = c.getMethod("getContext"); Object obj = m.invoke(service); if (Context.class.isAssignableFrom(obj.getClass())) { ctx = (Context) obj; } else if (View.class.isAssignableFrom(obj.getClass())) { ctx = ((View) obj).unwrap(); } else { throw new IllegalArgumentException("Service doesn't have a context."); } } catch (NoSuchMethodException e) { throw new IllegalArgumentException(e); } catch (InvocationTargetException e) { throw new IllegalArgumentException(e); } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } return ctx; }
public static Module linkView(View view) { return linkView(view.unwrap().getId(), Suppliers.ofInstance(view)); }
public static Module linkView(View view) { return linkView(view.unwrap().getId(), Suppliers.ofInstance(view)); }
public static Module linkView(View view) { return linkView(view.unwrap().getId(), Suppliers.ofInstance(view)); }
/** * Extracts the Name of the Service. * * @param service * @param <T> * @return */ public static <T> String toName(T service) { String name = null; try { Context context = toContext(service); if (View.class.isAssignableFrom(context.getClass())) { name = ((View) context).unwrap().getName(); } else { name = context.getName(); } } catch (IllegalArgumentException ex) { //Fallback to pure reflection name = callMethod(service, "getName", String.class); } return name; }
/** * Extracts the Id of the Service. * * @param service * @param <T> * @return */ public static <T> String toId(T service) { String id = null; try { Context context = toContext(service); if (View.class.isAssignableFrom(context.getClass())) { id = ((View) context).unwrap().getId(); } else { id = context.getId(); } } catch (IllegalArgumentException ex) { //Fallback to pure reflection id = callMethod(service, "getId", String.class); } return id; }
/** * Extracts the Name of the Service. * * @param service * @param <T> * @return */ public static <T> String toName(T service) { String name = null; try { Context context = toContext(service); if (View.class.isAssignableFrom(context.getClass())) { name = ((View) context).unwrap().getName(); } else { name = context.getName(); } } catch (IllegalArgumentException ex) { //Fallback to pure reflection name = callMethod(service, "getName", String.class); } return name; }
/** * Extracts the Id of the Service. * * @param service * @param <T> * @return */ public static <T> String toId(T service) { String id = null; try { Context context = toContext(service); if (View.class.isAssignableFrom(context.getClass())) { id = ((View) context).unwrap().getId(); } else { id = context.getId(); } } catch (IllegalArgumentException ex) { //Fallback to pure reflection id = callMethod(service, "getId", String.class); } return id; }